Author Topic: Hook - OnPlayerChat  (Read 3112 times)

0 Members and 1 Guest are viewing this topic.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Hook - OnPlayerChat
« 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:



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

Offline Cobalt

  • Full Member
  • ***
  • Posts: 216
  • Karma: 44
  • http://steamcommunity.com/id/__yvl/
Re: Hook - OnPlayerChat
« Reply #1 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.

Offline Avoid

  • Full Member
  • ***
  • Posts: 142
  • Karma: 42
Re: Hook - OnPlayerChat
« Reply #2 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 )
« Last Edit: July 18, 2014, 02:40:53 PM by Avoid »

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Re: Hook - OnPlayerChat
« Reply #3 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 )
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Hook - OnPlayerChat
« Reply #4 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

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Re: Hook - OnPlayerChat
« Reply #5 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.
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Hook - OnPlayerChat
« Reply #6 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 :)

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Hook - OnPlayerChat
« Reply #7 on: July 20, 2014, 03:14:10 AM »
Unlocking this thread for a quick question;

concommand.Add is serverside right?

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Re: Hook - OnPlayerChat
« Reply #8 on: July 20, 2014, 03:21:31 AM »
concommand.Add is shared (both client and server).
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline Zmaster

  • Full Member
  • ***
  • Posts: 235
  • Karma: 25
Re: Hook - OnPlayerChat
« Reply #9 on: July 20, 2014, 10:01:37 AM »
Oh, alright, thanks again :P