Bringing !donate to life

This is probably already been asked… but I’m asking it again. I want to make a few ! commands in evolve. But I’ve never really figured out how. I found one post with a link to a http://wiki.garrysmod.com/ page. But it was deleted. So I’m starting on a fresh.

Just do something like this,

(Serverside)
[lua]
ChatCommands = {}

function AddChatCommand(name, func)
ChatCommands[name] = func
end

local function _PlayerSay(ply, text)
local prefix = text:sub(1,1)
local command = text:sub(2)
local args = string.Explode(" ", command)

if prefix == "!" and not (command == "") and not (args[1] == "") then
	local oargs = table.Copy(args)
	local cmd = oargs[1]
	args[1] = nil
	
	if not ChatCommands[cmd:lower()] then
		ply:ChatPrint("That command does not exist!")
	else
		local c = ChatCommands[cmd:lower()]
		c(ply,args)
	end
	return ""
end

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

Then you can add commands like,

(Serverside)
[lua]
AddChatCommand(“credits”, function(ply,args)
print("Hey " … ply:Nick())
end)
[/lua]

That’s one way, here’s one that’s slightly more universal because you only have to add the console command instead of editing the PlayerSay file any more.

Add this snippet in there near the top

[lua] //
// Do /commands privately, !commands publicly
//
– local _bSlash = string.sub( text, 1, 1 ) == “/”;
– local _bExclaim = string.sub( text, 1, 1 ) == “!”;
local _bSlash = string.StartWith( text, “/” );
local _bExclaim = string.StartWith( text, “!” );
if ( _bSlash || _bExclaim ) then
Player:ConCommand( string.sub( text, 2 ) );
if ( !_bExclaim ) then
return “”;
end
end[/lua]

The commented out boolean for slash and exlaim to the same thing as the other, it’s an alternative to learn.

The reason I recommend this is because no one should be using console commands anymore with the net library being around. Use net for important things, and then anything the player should be able to use in chat such as !motd or /motd should be added via a standard console command.

If you want to make these concommands special, you could add a prefix to those concommands like so:

[lua]Player:ConCommand( “chat_command_” … string.sub( text, 2 ) );[/lua]

And then if you really want the AddChatCommand…
[lua]function AddChatCommand( _cmd, func )
concommand.Add( "chat_command
" … _cmd, function( _p, _cmd, _args ) _func( _p, _cmd, _args ); end );
end[/lua]

Either one will work fine for what you want it to do, use either, and use the other to learn from :slight_smile: