Hey everyone,
Just wondering. Anyone know the command to make chat commands from lets just say...console commands?
What?? Do you want how to use commands in chat like !pm MaZy Hi or !kick MaZy ??
Exactly. Something like /pm !pm
Ah, here we are
[URL]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index8519.html[/URL]
Turns out there's a hook that gets triggered when a player says something. You can do something like;
[CODE]
function chatCommand( ply, text, public )
if (string.sub(text, 1, 4) == "/die") then --if the first 4 letters are /die, kill him
ply:Kill()
return(false) --Hides the "/die" from chat
end
end
hook.Add( "PlayerSay", "chatCommand", chatCommand );
[/CODE]
which will kill a player whenever he says "/die" in chat.
Thanks so much!
No probs
[lua]
local chatcommands = {};
function GM:PlayerSay(ply, text, teamc, alive)
local chat_string = string.Explode(" ", text);
for k, v in pairs( chatcommands ) do
if( chat_string[1] == k ) then
table.remove(chat_string, 1);
v(ply, chat_string);
return "";
end
if( string.find(k, chat_string[1]) != nil ) then
local start, endp, word = string.find(k, chat_string[1]);
if( endp - (start - 1) > 2 ) then
ply:ChatPrint("Invalid command! Did you mean '"..tostring( k ).."'?");
return "";
end
end
end
return text;
end
function RegisterChatCommand(strCommand, Func)
if( !strCommand || !Func ) then return; end
for k, v in pairs( chatcommands ) do
if( strCommand == k ) then
return;
end
end
chatcommands[ tostring( strCommand ) ] = Func;
end
[/lua]
here is my chatcommand API (credits to blackbird for influencing it).
All you have to do is RegisterChatCommand("!CommandName", function(ply, --[[table]]args) /*do stuff here*/ end) and you're good to go. Here's an example:
[lua]
RegisterChatCommand("!fuckyou", function(ply, args)
if( ply:IsAdmin() ) then
local pname = args[1];
for _, v in pairs( player.GetAll() ) do
if( string.find(v:Nick(), pname) ) then
v:ChatPrint("Hey man, fuck you.");
end
end
end
end)
[/lua]
[editline]31st August 2012[/editline]
And just for the record, if you misspell a chat command when running it, it will suggest the closest command registered.
[QUOTE=JustSoFaded;37490686][lua]
local chatcommands = {};
function GM:PlayerSay(ply, text, teamc, alive)
local chat_string = string.Explode(" ", text);
for k, v in pairs( chatcommands ) do
if( chat_string[1] == k ) then
table.remove(chat_string, 1);
v(ply, chat_string);
return "";
end
if( string.find(k, chat_string[1]) != nil ) then
local start, endp, word = string.find(k, chat_string[1]);
if( endp - (start - 1) > 2 ) then
ply:ChatPrint("Invalid command! Did you mean '"..tostring( k ).."'?");
return "";
end
end
end
return text;
end
function RegisterChatCommand(strCommand, Func)
if( !strCommand || !Func ) then return; end
for k, v in pairs( chatcommands ) do
if( strCommand == k ) then
return;
end
end
chatcommands[ tostring( strCommand ) ] = Func;
end
[/lua]
here is my chatcommand API (credits to blackbird for influencing it).
All you have to do is RegisterChatCommand("!CommandName", function(ply, --[[table]]args) /*do stuff here*/ end) and you're good to go. Here's an example:
[lua]
RegisterChatCommand("!fuckyou", function(ply, args)
if( ply:IsAdmin() ) then
local pname = args[1];
for _, v in pairs( player.GetAll() ) do
if( string.find(v:Nick(), pname) ) then
v:ChatPrint("Hey man, fuck you.");
end
end
end
end)
[/lua]
[editline]31st August 2012[/editline]
And just for the record, if you misspell a chat command when running it, it will suggest the closest command registered.[/QUOTE]
Many thanks!! Lua King! I just found this and was something that I was looking for! Ill give you credit! Many thanks!!
Wait, so how would I do this with, like, bringing up Derma Menus?
[QUOTE=iGarrettWM;42575640]Wait, so how would I do this with, like, bringing up Derma Menus?[/QUOTE]
Look at the date before posting
Can i use it for other gamemodes? Like, Murder or anything else?
Sorry, you need to Log In to post a reply to this thread.