Chat commands

How would I make something like !help run a command in console?

**[String.gsub

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=String.gsub)**
Use this.

Sorry I need more then that… How would I get what a player has said…

PlayerSay hook.

[lua]local chatcmd = “!slay”

function FindPlayer(name)
local ret = {}
for k , v in pairs(player.GetAll()) do
if string.find(string.lower(v:Nick()) , string.lower(name)) then
table.insert(ret , v)
end
end
return ret
end

function SlayPeople(pl , text , team , dead)
if pl:IsAdmin() then
if string.sub(string.lower(text) , 0 , string.len(chatcmd)) == chatcmd then
local players = {}
for k , v in pairs(string.Explode("," , string.sub(text , string.len(chatcmd)))) do
if FindPlayer(name) then
table.insert(players , FindPlayer(name))
end
end

		local pls = {}
		for a , b in pairs(players) do
			if type(b) == "table" then
				for c , d in pairs(b) do
					table.insert(pls , d)
				end
			end
		end
		
		local names = {}
		for k , v in pairs(pls) do
			table.insert(names , v:Nick())
			v:Kill()
		end
		
		for k , v in pairs(player.GetAll()) do
			v:ChatPrint("Admin "..pl:Nick().." slayed "..table.concat("," , names))
		end
	end
end

end
hook.Add(“PlayerSay” , “Slay” , SlayPeople)[/lua]

Untested messy example.

You need to hook into **[Gamemode.PlayerSay

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.PlayerSay)** to get what a player has said. Then you need to check if the text contains the chatcommand and, if so, call the function.

[editline]08:35PM[/editline]

:ninja:

Should be less messy example:

[lua]local function Stuff(ply, text)
local cmd = string.Explode(" ", text)[1]
if cmd == “!stuff” then – If the player said !slay <anything> then:
ply:Slay() – Kill him
ply:ChatPrint(string.sub(string.len(cmd) + 2, string.len(text))) – Print what he said after !slay, like if i did !slay minge then it would print minge
ply:ConCommand(“say”, “hello! i just said !stuff”)
return “” – What it should replace text with, in this case if we say !stuff it will hide it, if this was: return “foo” then it would be Minge: foo instead of Minge: !stuff, returning an empty string means it hides the message
end
end
hook.Add(“PlayerSay” , “Stuff” , Stuff) – Hook it[/lua]

Basic chatcommand stuff, i hope the comments didnt get too messy

[editline]11:02PM[/editline]

holy shit they got too messy

You’re attempting to compare a table (cmd) o a string on line 3.
It also wouldn’t slay anyone but yourself. You didn’t do anything to check who the second argument was.

Basically just use the first example. It works perfectly.

Fixed mine, first example works but its very messy

There’s a reason I made this.
http://www.facepunch.com/showthread.php?t=927717