Console command

Tell me how to start the chat console command?

Chat console command? What

Creating a console command:

concommand.Add (Look at example)
Creating a chat command:

GM/PlayerSay (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]


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)


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.

not work


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)

Where is the file located?

garrysmod/lua/autorun/client

These are both serverside code, put them in garrysmod/lua/autorun/server/file.lua


ply:ConCommand("kill")


FCVAR_SERVER_CAN_EXECUTE prevented server running command:  kill


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: https://dl.dropboxusercontent.com/u/26074909/tutoring/chat_commands/chat_commands.lua.html

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: https://dl.dropboxusercontent.com/u/26074909/tutoring/chat_commands/chat_commands_examples.lua.html and https://dl.dropboxusercontent.com/u/26074909/tutoring/chat_commands/chat_commands_other.lua.html



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 );

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( )…

Could you help me?



concommand.Add( "ttt", function OnPlayerChat( Player )
		LocalPlayer():ConCommand("connect ip:port")
end )


I do not understand how you can do it.

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

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…

https://dl.dropboxusercontent.com/u/26074909/tutoring/chat_commands/chat_commands.lua

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]

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…
https://dl.dropboxusercontent.com/u/26074909/tutoring/chat_commands/chat_commands.lua