Chat console command? What
Creating a console command: [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/concommand/Add]concommand.Add[/url] (Look at example)
Creating a chat command: [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/PlayerSay]GM/PlayerSay[/url] (Look at example 2)
However, if you mean to run a console command through a chat command, follow example 2 on making a chat command, then use ConCommand to run a console command on the player, such as:
[lua]player:ConCommand("say Hello!")[/lua]
[QUOTE=Author.;47177819]Chat console command? What
Creating a console command: [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/concommand/Add]concommand.Add[/url] (Look at example)
Creating a chat command: [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/PlayerSay]GM/PlayerSay[/url] (Look at example 2)
However, if you mean to run a console command through a chat command, follow example 2 on making a chat command, then use ConCommand to run a console command on the player, such as:
[lua]player:ConCommand("say Hello!")[/lua][/QUOTE]
[CODE]concommand.Add("killyourself", function( ply )
ply:Kill()
print("You killed yourself!")
end)
hook.Add( "PlayerSay", "killyourself", function(ply, text, public)
text = string.lower(text) -- Make the chat message entirely lowercase
if (string.sub(text, 1) == "!kill") then
ply:Kill()
return false
end
end)
[/CODE]
What's wrong here?
It should be string.sub( text, 1, 5 )
[editline]20th February 2015[/editline]
Also, PlayerSay is a serverside hook only so make sure you're running it in that realm.
[QUOTE=code_gs;47178018]It should be string.sub( text, 1, 5 )
[editline]20th February 2015[/editline]
Also, PlayerSay is a serverside hook only so make sure you're running it in that realm.[/QUOTE]
not work
[CODE]concommand.Add("killyourself", function( ply )
ply:Kill()
print("You killed yourself!")
end)
hook.Add( "PlayerSay", "killyourself", function(ply, text, public)
text = string.lower(text) -- Make the chat message entirely lowercase
if (string.sub( text, 1, 5 ) == "!kill") then
ply:Kill()
return false
end
end)[/CODE]
Where is the file located?
garrysmod/lua/autorun/client
These are both serverside code, put them in garrysmod/lua/autorun/server/file.lua
[code]ply:ConCommand("kill")[/code]
[code]FCVAR_SERVER_CAN_EXECUTE prevented server running command: kill
[/code]
How to run from a client?
string.sub also works by just using 1 number ( start ) and it'll then go to the end. It's useful for extension by simply using -x as start instead of using string.GetExtensionFromFileName or whatever...
If you want to turn all ! and / in chat to console commands, take a look at this: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/chat_commands/chat_commands.lua.html[/url]
Example 1 redirects them to console commands where ! is output for all to see and / commands are private.
Example 2 shows another method which calls a console command with salt and pepper ( prefix and postfix ) and gives you a command to use to add chat commands.. chat.AddCommand so that it'll make console commands with the same prefix and postfix...
Other examples show how to hard-code commands: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/chat_commands/chat_commands_examples.lua.html[/url] and [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/chat_commands/chat_commands_other.lua.html[/url]
[QUOTE=Acecool;47178952]string.sub also works by just using 1 number ( start ) and it'll then go to the end. It's useful for extension by simply using -x as start instead of using string.GetExtensionFromFileName or whatever...
If you want to turn all ! and / in chat to console commands, take a look at this: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/chat_commands/chat_commands.lua.html[/url]
Example 1 redirects them to console commands where ! is output for all to see and / commands are private.
Example 2 shows another method which calls a console command with salt and pepper ( prefix and postfix ) and gives you a command to use to add chat commands.. chat.AddCommand so that it'll make console commands with the same prefix and postfix...
Other examples show how to hard-code commands: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/chat_commands/chat_commands_examples.lua.html[/url] and [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/chat_commands/chat_commands_other.lua.html[/url][/QUOTE]
[code]
hook.Add( "PlayerSay", "ttt", function ( Player, text, private )
local _bSlash = string.StartWith( text, "/" );
local _bExclaim = string.StartWith( text, "!" );
if ( _bSlash || _bExclaim ) then
// This executes the command
Player:ConCommand( string.sub( text, 2 ) );
// This hides / commands from being displayed
if ( !_bExclaim ) then
return "";
end
end
end );
concommand.Add( "ttt", function( ply )
ply:ConCommand("connect 179.60.147.48:27019")
end );[/code]
FCVAR_SERVER_CAN_EXECUTE prevented server running command: connect
You could also network the command to the client to have the client execute it ( there is one method which lets you use connect unless they finally blocked it ).. Alternatively, you could use OnPlayerChat to detect the commands too and then it'd be on the CLIENT. Make sure you check the player that sent the message is LocalPlayer( )...
[QUOTE=Acecool;47179094]You could also network the command to the client to have the client execute it ( there is one method which lets you use connect unless they finally blocked it ).. Alternatively, you could use OnPlayerChat to detect the commands too and then it'd be on the CLIENT. Make sure you check the player that sent the message is LocalPlayer( )...[/QUOTE]
Could you help me?
[code]
concommand.Add( "ttt", function OnPlayerChat( Player )
LocalPlayer():ConCommand("connect ip:port")
end )
[/code]
I do not understand how you can do it.
[QUOTE=giraff;47179201]Could you help me?
[code]
concommand.Add( "ttt", function OnPlayerChat( Player )
LocalPlayer():ConCommand("connect ip:port")
end )
[/code]
I do not understand how you can do it.[/QUOTE]
Try using RunConsoleCommand("connect","ip:port")
Clientside.
[lua]concommand.Add("ttt", function(ply, cmd, args)
RunConsoleCommand("connect", "IP:PORT")
end)[/lua]
Replace IP and PORT with IP and Port, and add this to lua/autorun/CLIENT/file.lua
[QUOTE=Author.;47179430][lua]concommand.Add("ttt", function(ply, cmd, args)
RunConsoleCommand("connect", "IP:PORT")
end)[/lua]
Replace IP and PORT with IP and Port, and add this to lua/autorun/CLIENT/file.lua[/QUOTE]
How do I run it from the chat?
I just added Example 3, networking the command so the client executes it from the client. I haven't tested it so I don't know if I needed to use string.sub( _msg, 2, _end or _end + 1 ), for the command... Or the arg.. Test it out, I'd recommend adding a print statement.
I only added the string.sub because exploding then imploding on 2 + didn't make sense... because RunConsoleCommand uses 2 args, while ConCommand uses 1....
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/chat_commands/chat_commands.lua[/url]
You'd use concommand.Add on the client, or server depending where the code needs to run, and that's it...
I'll add Example 4 in a bit, the Clientside only version using OnPlayerChat...
[editline]20th February 2015[/editline]
[QUOTE=giraff;47179576]How do I run it from the chat?[/QUOTE]
Alright, it is updated... See example 4 for 100% client-side chat-command system... See Note 1 at the bottom, or Example 3 to see how to separate _cmd and _args if you use RunConsoleCommand...
[url]https://dl.dropboxusercontent.com/u/26074909/tutoring/chat_commands/chat_commands.lua[/url]
Sorry, you need to Log In to post a reply to this thread.