luagarrys-mod

Can't use Hook PlayerSay in GMod


I tryed out my Addon on a Test Server from IPS Hosting and my PlayerSay Hook doesn't work anymore. If I try it on my Local Test Server, then it works.

hook.Add("PlayerSay", "Testing", function(ply, text)
    if string.lower(text) == "/test" then
        print("test")
    end
end)

Solution

  • PlayerSay is a server side hook. If you're not seeing the print in the server console after writting "/test" in the chat then it's because the server it's not running that code. To make sure the server runs that piece of code your file must be included to a file in the autorun folder or must be moved to the autorun folder.

    Also, make sure only the server runs that piece of code, because the client doesn't have that hook and will return a Lua error if that code runs on the client.

    if SERVER then -- Only run if server and not client
        hook.Add("PlayerSay", "Testing", function(ply, text)
            if string.lower(text) == "/test" then
                print("test")
            end
        end)
    end
    

    Let me know if you still don't understand something and I'll edit my answer.