Ulysses

General => Developers Corner => Topic started by: Zmaster on July 18, 2014, 02:12:03 PM

Title: Hook - OnPlayerChat
Post 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:
Code: [Select]
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"
Title: Re: Hook - OnPlayerChat
Post by: Cobalt on July 18, 2014, 02:28:25 PM
Put the parentheses after the last end instead of on line 67. Also, don't use return in a chat hook.
Title: Re: Hook - OnPlayerChat
Post by: Avoid on July 18, 2014, 02:32:29 PM
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: [Select]
function ShowRules( pl, text, teamonly )
        if (string.lower(text) == "!rules") then
                pl:ConCommand("bvgrules")
        end
end
hook.Add( "PlayerSay", "Rules", ShowRules )
Title: Re: Hook - OnPlayerChat
Post by: Decicus on July 18, 2014, 02:37:17 PM
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: [Select]
function ShowRules( pl, text, teamonly )
        if (string.lower(text) == "!rules") then
                pl:ConCommand( "bvgrules" )
        end
end
hook.Add( "PlayerSay", "Rules", ShowRules )
Title: Re: Hook - OnPlayerChat
Post by: Zmaster on July 18, 2014, 05:30:03 PM
Okay, I used this as you guys suggested
Code: [Select]
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
Title: Re: Hook - OnPlayerChat
Post by: Decicus on July 18, 2014, 05:50:27 PM
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.
Title: Re: Hook - OnPlayerChat
Post by: Zmaster on July 18, 2014, 06:35:43 PM
No I had it in lua/autorun/client

Moved it to lua/autorun/server and it works fine now, thanks for the help, guys :)
Title: Re: Hook - OnPlayerChat
Post by: Zmaster on July 20, 2014, 03:14:10 AM
Unlocking this thread for a quick question;

concommand.Add is serverside right?
Title: Re: Hook - OnPlayerChat
Post by: Decicus on July 20, 2014, 03:21:31 AM
concommand.Add is shared (both client and server).
Title: Re: Hook - OnPlayerChat
Post by: Zmaster on July 20, 2014, 10:01:37 AM
Oh, alright, thanks again :P