• Creating a chat command?
    6 replies, posted
I want to create a new chat command, on a specific Target, how do I do that?
Will there are two hooks that are very useful. PlayerSay and ChatText. PlayerSay is handled on the server-side, whilst ChatText is client-side. If you want to open a derma menu, or anything client-sided, use ChatText: [CODE] hook.Add( "ChatText", "blabla", function( in, txt ) if txt == "!test" then --> If the text the player typed up is "!test" then continue RunConsoleCommand( "kill" ) --> Run the console command "kill" end --> End of the if statement end ) [/CODE] But if you want to make a chat command that is more server-sided, use PlayerSay: [CODE] hook.Add( "PlayerSay", "blablabla", function( ply, text ) if text == "!test" then --> If the text the player typed up is "!test" then continue ply:SetWalkSpeed( 999 ) --> Set the player that typed the commands walk speed to 999 end --> End of if statement. end ) [/CODE]
[QUOTE=Tupac;49884799]Will there are two hooks that are very useful. PlayerSay and ChatText. PlayerSay is handled on the server-side, whilst ChatText is client-side. If you want to open a derma menu, or anything client-sided, use ChatText: [CODE] hook.Add( "ChatText", "blabla", function( in, txt ) if txt == "!test" then --> If the text the player typed up is "!test" then continue RunConsoleCommand( "kill" ) --> Run the console command "kill" end --> End of the if statement end ) [/CODE] But if you want to make a chat command that is more server-sided, use PlayerSay: [CODE] hook.Add( "PlayerSay", "blablabla", function( ply, text ) if text == "!test" then --> If the text the player typed up is "!test" then continue ply:SetWalkSpeed( 999 ) --> Set the player that typed the commands walk speed to 999 end --> End of if statement. end ) [/CODE][/QUOTE] ChatText is an awful hook to use, and you didn't use the correct arguments for it {You checked for the player name being "!test"...) [quote][I]Note, that this isn't working with player messages even though there are arguments for it. For player messages see GM:PlayerSay and GM:OnPlayerChat[/I][/quote] And just as the wiki says, use [URL="http://wiki.garrysmod.com/page/GM/OnPlayerChat"]OnPlayerChat[/URL] for a client-side hook. [lua]hook.Add( "OnPlayerChat", "MyChatCommandIdentifier", function( ply, text ) if ply == LocalPlayer() then if text = "!MyChatCommand" then --Do shit end end end )[/lua]
Example for serverside chat commands [lua]local chatcmds={} --Table of chat commands local function GetPlayersByNick(nick) local result={} for _,ply in pairs(player.GetAll()) do if(string.find(string.lower(ply:Nick()),nick)) then table.insert(result,ply) end end return result --Table of players with this name end hook.Add("PlayerSay","ChatCommand",function(ply,txt) if txt[1]!="!" or #txt==1 then return end --Not a command local args=string.Explode(" ",txt:sub(2)) local cmd=args[1]:lower() --Name of the command if chatcmds[cmd] then --Check if command exists table.remove(args,1) return chatcmds[cmd](ply,args) --Call function for this command end --Invalid command end) function chatcmds.kill(ply,args) --Adds a simple !kill <player name> chat command if(#args==0) then return end --Forgot to enter a name local players=GetPlayersByNick(string.lower(table.concat(args," "))) --Table of players with this name if #players==1 and ply:IsAdmin() and players[1]:Alive() then players[1]:Kill() end end[/lua]
I need it [B]On a specific Target![/B]
[QUOTE=GodGuardian;49893225]I need it [B]On a specific Target![/B][/QUOTE] There are dozens of ways to refer to a player. How do you want to locate the target?
Done, I just found another one on the internet. A one that actually answers what I need.
Sorry, you need to Log In to post a reply to this thread.