Help with chat commands

I am currently working on chat commands and I tried this code underneath. It doesn’t seem to work though. I don’t know why :S.


function myCommand( ply, text, public )
	if( string.sub( text, 1, 7) == "!warden" ) then
		print ( "works" )
	end
end

hook.Add( "PlayerSay", "myCommand", myCommand )

This is pretty dirty, but it works :slight_smile:


if SERVER then
	AddCSLuaFile()
	return
end

local function ChatGUI(pl, text)
	if string.sub(text, 1, 7) == "!donate" then
		if pl != LocalPlayer() then return true end
		gui.OpenURL("http://donate.uppercutservers.com")
	elseif string.sub(text, 1, 6) == "!group" or string.sub(text, 1, 5) == "!steam" or string.sub(text, 1, 5) == "!join" then
		if pl != LocalPlayer() then return true end
		gui.OpenURL("http://steamcommunity.com/groups/uppercutservers")
	elseif string.sub(text, 1, 6) == "!forum" then
		if pl != LocalPlayer() then return true end
		gui.OpenURL("http://uppercutservers.com")
	end
end
hook.Add("OnPlayerChat", "ChatGUI", ChatGUI)


function myCommand( ply, text, public )
	if( string.sub( text, 1, 6) == "!warden" ) then
		print ( "works" )
	end
end

hook.Add( "PlayerSay", "myCommand", myCommand )

Only 6 letters in warden.

7 with the ! .

Also since your printing, do you check your console or server console?

This should also work:
[lua]function warden(ply,text,public)
if( string.sub( text, 1, 7) == “!warden” ) then
ply:ChatPrint(“This Works”) – To Player containted in the variable
print(“This Works!”) – Server Side
end
end
hook.Add(“PlayerSay”,“warden”,warden)
[/lua]

I’d say sting.Left would be better when checking for chat commands.

Also, they way I like to check for commands is like this:

[LUA]
hook.Add(“PlayerSay”, “checkcommands”, function( ply, said )
if string.Left( said, 1 ) == “!” then
local split = string.Split(said, " " )
local firstword = split[1]
local command = string.sub( firstword, 2, string.len( firstword ) )
– Now you can do some ifs with command, eg
if command == “warden” then
print(“warder”)
end
– It’s a good idea to return false on commands too in my opinion
– A lot clearner chat
return false
end
end)
[/LUA]

Means you don’t have to string sub evertime, because command is always the first word, minus the ‘!’

Of course, if you don’t plan to check for other commands, simple string.sub would be adequate.
This way is useful if you want to check a few commands, and don’t have to redo subs all the time.

Good eye. Surprised I didn’t catch that.

I pretty much got this working now. Thanks for all the replies!