Ulysses
General => Developers Corner => Topic started by: Zmaster on July 18, 2014, 02:12:03 PM
-
I'm trying to create my own chat command (for learning purposes) that will run a console command.
Here's the code for it:
hook.Add( "OnPlayerChat", "TheRules", function( p, t, tc, isD ) )
if p == LocalPlayer() and (t=="!rules") then
RunConsoleCommand( "bvgrules" )
else return end
end
Before anyone asks, the console command (bvgrules) is working perfectly fine. When I type that in the console (not trying the chat command) it opens the DPanel perfectly fine. That's not the problem.
Here's the error I get when I try to run the code above:
(http://puu.sh/ahdaB/6c11110ad2.png)
Just for reference, line 67 is the line that starts with "hook.Add"
-
Put the parentheses after the last end instead of on line 67. Also, don't use return in a chat hook.
-
You could try enclosing the end at the bottom with a ) since you are adding to a hook. - Check your error message!
A different approach would be: (Not sure if that works, please test it for yourself!)
function ShowRules( pl, text, teamonly )
if (string.lower(text) == "!rules") then
pl:ConCommand("bvgrules")
end
end
hook.Add( "PlayerSay", "Rules", ShowRules )
-
You also have a ")" on the end of the "hook.Add" line. As Cobalt said, move that to after the "end".
You can also use PlayerSay as Avoid said, although PlayerSay is a serverside hook, so you would do "pl:ConCommand( "bvgrules" )" instead of RunConsoleCommand. It would look like this (using Avoid's code):
function ShowRules( pl, text, teamonly )
if (string.lower(text) == "!rules") then
pl:ConCommand( "bvgrules" )
end
end
hook.Add( "PlayerSay", "Rules", ShowRules )
-
Okay, I used this as you guys suggested
function ShowRules( pl, text, teamonly )
if (string.lower(text) == "!rules") then
pl:ConCommand("bvgrules")
end
end
hook.Add( "PlayerSay", "Rules", ShowRules )
Now when I type "!rules", it doesn't give any Lua errors, but it does nothing at all
-
Did you put the function inside lua/autorun/server?
PlayerSay is a serverside hook, therefore it needs to be put into the serverside Lua section.
-
No I had it in lua/autorun/client
Moved it to lua/autorun/server and it works fine now, thanks for the help, guys :)
-
Unlocking this thread for a quick question;
concommand.Add is serverside right?
-
concommand.Add is shared (both client and server).
-
Oh, alright, thanks again :P