General > Developers Corner

Hook - OnPlayerChat

(1/2) > >>

Zmaster:
I'm trying to create my own chat command (for learning purposes) that will run a console command.

Here's the code for it:

--- Code: ---hook.Add( "OnPlayerChat", "TheRules", function( p, t, tc, isD ) )
if p == LocalPlayer() and (t=="!rules") then
RunConsoleCommand( "bvgrules" )
else return end
end
--- End code ---

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:



Just for reference, line 67 is the line that starts with "hook.Add"

Cobalt:
Put the parentheses after the last end instead of on line 67. Also, don't use return in a chat hook.

Avoid:
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!)

--- Code: ---function ShowRules( pl, text, teamonly )
        if (string.lower(text) == "!rules") then
                pl:ConCommand("bvgrules")
        end
end
hook.Add( "PlayerSay", "Rules", ShowRules )

--- End code ---

Decicus:
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):

--- Code: ---function ShowRules( pl, text, teamonly )
        if (string.lower(text) == "!rules") then
                pl:ConCommand( "bvgrules" )
        end
end
hook.Add( "PlayerSay", "Rules", ShowRules )

--- End code ---

Zmaster:
Okay, I used this as you guys suggested

--- Code: ---function ShowRules( pl, text, teamonly )
        if (string.lower(text) == "!rules") then
                pl:ConCommand("bvgrules")
        end
end
hook.Add( "PlayerSay", "Rules", ShowRules )
--- End code ---

Now when I type "!rules", it doesn't give any Lua errors, but it does nothing at all

Navigation

[0] Message Index

[#] Next page

Go to full version