• How to make a chat command do something.
    7 replies, posted
Hi guys, I was wondering how can I make a chat command do something, lets say I type !sam kill Wilki It will run kill on the name. That is all I need to know, once I know that I can work it out for myself.
[lua]function ChatCommands(pl,text,toall) local args = string.Explode(" ",text) -- DO stuff with args, like you would a concommand end hook.Add("PlayerSay","ChatCommands",ChatCommands)[/lua]
So would I use [lua]function ChatCommands(pl,text,toall) local args = string.Explode(" ",kill) pl:Kill end hook.Add("PlayerSay","ChatCommands",ChatCommands)[/lua]
[lua] hook.Add("PlayerSay", "MyCommandsThing", function(plyObject, saidText, isPublic) local arguments = string.Explode(" ", saidText); -- Seperate the text we typed by spaces. -- Did we type '!kill' and did we type anything after that? if (arguments[1] == "!kill" and arguments[2] and arguments[2] != "") then -- Excellent, now let's find a player to kill! local findName = string.lower( arguments[2] ); -- Put the string we're searching for in lower-case. local killedPlayers = {}; -- A table to store who we've killed. -- Loop through every player, we're gonna try and match names with what we typed. for k, v in ipairs( player.GetAll() ) do -- Does this player's lower-case name contain what we're looking for? if ( string.find(string.lower( v:Name() ), findName) ) then -- Great! We've got our player, let's kill 'em! v:Kill(); -- Now let's put them into our killed players table. killedPlayers[#killedPlayers + 1] = v:Name(); end; end; -- Did we kill any players? if (#killedPlayers > 0) then -- Send a message to everyone! for k, v in ipairs( player.GetAll() ) do v:PrintMessage(3, "[MyCommandsThing] "..plyObject:Name().." has killed "..table.concat(killedPlayers, ", and ").."."); end; end; end; end); [/lua] I haven't tested it though.
How can I stop it from showing the command in chat?
Return an empty string between lines 28 and 29 (In Conna's code). [editline]02:03PM[/editline] [lua]hook.Add("PlayerSay", "MyCommandsThing", function(plyObject, saidText, isPublic) local arguments = string.Explode(" ", saidText); -- Seperate the text we typed by spaces. -- Did we type '!kill' and did we type anything after that? if (arguments[1] == "!kill" and arguments[2] and arguments[2] != "") then -- Excellent, now let's find a player to kill! local findName = string.lower( arguments[2] ); -- Put the string we're searching for in lower-case. local killedPlayers = {}; -- A table to store who we've killed. -- Loop through every player, we're gonna try and match names with what we typed. for k, v in ipairs( player.GetAll() ) do -- Does this player's lower-case name contain what we're looking for? if ( string.find(string.lower( v:Name() ), findName) ) then -- Great! We've got our player, let's kill 'em! v:Kill(); -- Now let's put them into our killed players table. killedPlayers[#killedPlayers + 1] = v:Name(); end end -- Did we kill any players? if (#killedPlayers > 0) then -- Send a message to everyone! for k, v in ipairs( player.GetAll() ) do v:PrintMessage(3, "[MyCommandsThing] "..plyObject:Name().." has killed "..table.concat(killedPlayers, ", and ").."."); end end return ""; end end); [/lua]
Whoops, forgot that. Cheers.
EDIT: I'm stupid
Sorry, you need to Log In to post a reply to this thread.