Lua Problems With GMOD Custom Chat Script

2.1k Views Asked by At

So I am currently trying to create a small script for when a player, in GMOD, enters "!content" for example, they would be presented by a window that would lead them to the Steam Content for that server. For the previous example it worked so I then tried to copy over the template and just change the function name etc. I was not met with the same result as the example shown, yet nothing seem to happen and im not sure why as I have only changed the function name and string. If you can please help me that would be great. Thanks in advance.

Steam Group Chat Script (Works)

function steamgroupCommand( ply, text)  
    if string.sub( text, 1, 6 ) == "!steam" then  
    ply:PrintMessage( 3, "It Worked!" )  
    ply:SendLua([[gui.OpenURL("http://steamcommunity.com/groups/PhantomNetworksCommunity")]])  
    for k, v in pairs(player.GetAll()) do v:ChatPrint( "Player " .. ply:Nick() .. " has used !steam to view our community Steam Group!" )  
    end  
    end  
end  
hook.Add( "PlayerSay", "Chat", steamgroupCommand )

Discord Chat Script (Does Not Work)

function discordCommand( ply, text)  
    if string.sub( text, 1, 8 ) == "!discord" then  
    ply:PrintMessage( 3, "It Worked!" )  
    ply:SendLua([[gui.OpenURL("https://discord.gg/35rQxcE")]])  
    for k, v in pairs(player.GetAll()) do v:ChatPrint( "Player " .. ply:Nick() .. " has used !discord to view our official Discord server!" )  
    end  
    end  
end  
hook.Add( "PlayerSay", "Chat", discordCommand )
3

There are 3 best solutions below

1
On

If you have a look at the documentation of hook.Add, you will see details about the needed arguments.

The second argument is important for you

  1. any identifier

The unique identifier, usually a string. This can be used elsewhere in the code to replace or remove the hook.

The identifier should be unique so that you do not accidentally override some other mods hook, unless that's what you are trying to do.

The identifier can be either a string, or a table/object with an IsValid function defined such as an Entity or Panel. numbers and booleans, for example, are not allowed.

If the identifier is a table/object, it will be inserted in front of the other arguments in the callback and the hook will be called as long as it's valid. However, as soon as IsValid( identifier ) returns false, the hook will be removed.

So according to this you need to change your second argument of hook.Add (which in your case is "chat" for both of your hooks) to be unique for each hook to not override your other hooks.

0
On

The issue is that you're overwriting the hook. You should change the second argument to something more descriptive such as:

hook.Add( "PlayerSay", "ChatSteamGroup", steamgroupCommand )

and hook.Add( "PlayerSay", "ChatDiscord", discordCommand )

0
On

This way will work, as this is what I do on my server.

if(CLIENT) then
    concommand.Add("steam", function()
        gui.OpenURL("http://steamcommunity.com/groups/PhantomNetworksCommunity")
    end)
end
if(SERVER) then
    hook.Add("PlayerSay", "OpenSteam", function(calling, txt, t)
        local args = string.lower(txt)
        if(args == "!steam") then
            calling:ConCommand("steam")
            return ""
        end
    end)
end

You can do this same thing on your discord invite.

EDIT:

I never realized you had it print to chat, so I will show you a good way to do it (or fix it):

if(CLIENT) then
    concommand.Add("steam", function()
        gui.OpenURL("http://steamcommunity.com/groups/PhantomNetworksCommunity")
    end)
    concommand.Add("discord", function()
        gui.OpenURL("https://discord.gg/35rQxcE")
    end)
    concommand.Add(".prt", function(calling, cmds, args, argStr)
        chat.AddText(Color(255, 255, 255), "Player", Color(98, 176, 255), args[1], Color(255, 255,255), " has used ", Color(255, 62, 62), "!steam", Color(255, 255, 255), " to view our community Steam Group!")
    end)
    concommand.Add(".disprt", function(calling, cmds, args, argStr)
        chat.AddText(Color(255, 255, 255), "Player", Color(98, 176, 255), args[1], Color(255, 255,255), " has used ", Color(255, 62, 62), "!discord", Color(255, 255, 255), " to view our official Discord server!")
    end)
end
if(SERVER) then
    hook.Add("PlayerSay", "ChatCommands", function(calling, txt, t)
        local args = string.lower(txt)
        if(args == "!steam") then
            calling:ConCommand("steam")
            for _, ply in pairs(player.GetAll()) do
                ply:ConCommand(".prt " .. calling:Nick())
            end
            return ""
        end
        if(args == "!discord") then
            calling:ConCommand("discord")
            for _, ply in pairs(player.GetAll()) do
                ply:ConCommand(".disprt " .. calling:Nick())
            end
            return ""
        end
    end)
end

EDIT 2:

Another possible reason for error is that both hooks have the same name, "Chat"