Connect to sever command.

Hello, recently I branched out into a TTT server and it isn’t getting much traffic. I would like to add a command in my Deathrun server that would allow players to connect to the TTT server. For example, they would type “!ttt” and it would automatically connect them to the ip I have set.

I’m not that experienced with coding but I can kinda understand it so if you could put things in simple terms that would be nice. Also i’m not asking you to make this script for me (although that would be nice) I would just like some links and tips that would help me create it.

[lua]
local servers = {
[“ttt”] = “0.0.0.0”,
[“deathrun”] = “1.1.1.1”
}

hook.Add( “PlayerSay”, “ChatCommand”, function( ply, text )
if ( string.sub( text, 1, 1) == “!” ) then
for command, address in pairs( servers ) do
if ( string.sub( text, 2, 1 + string.len( command ) ) == command ) then
ply:ConCommand( "connect " … address )
return false
end
end
end
end)
[/lua]

Something like this is what you’re looking for.

I executed the command in-game and it said in console “FCVAR_SERVER_CAN_EXECUTE prevented server running command: connect”

Ah sorry, I only tested it in single player, to make it run multiplayer you have to network stuff. I’ll show you how once I’ve had my dinner.

[editline]18th April 2015[/editline]

[lua]
local servers = {
[“ttt”] = “0.0.0.0”,
[“deathrun”] = “1.1.1.1”
}

hook.Add( “OnPlayerChat”, “ChatCommand”, function( ply, text )
if ply != LocalPlayer() then return end
if ( string.sub( text, 1, 1) == “!” ) then
for command, address in pairs( servers ) do
if ( string.sub( text, 2, 1 + string.len( command ) ) == command ) then
ply:ConCommand( "connect " … address )
return true
end
end
end
end)
[/lua]

This works clientside entierly, doesn’t require networking.

Sorry to get back to you late but I replaced the old code with the new code you gave me and its still saying “FCVAR_SERVER_CAN_EXECUTE prevented server running command: connect”

It works fine for me, where are you putting the code? Also, you shouldn’t be getting the FCVAR_SERVER_CAN_EXECUTE error because it isn’t being executed from the server (which brings me back to my first question).

Would you mind posting the code you are using, and where you are putting it?

Try using RunConsoleCommand( “connect”, address ) instead of ply:ConCommand.

Although that’s the better way to do it, it wouldn’t change anything.


local CATEGORY_NAME = "Custom Commands"

local servers = {
	["ttt"]			= "162.255.209.131",
	["TTT"]			= "162.255.209.131"
}

hook.Add( "OnPlayerChat", "ChatCommand", function( ply, text )
	if ply != LocalPlayer() then return end
	if ( string.sub( text, 1, 1) == "!" ) then
		for command, address in pairs( servers ) do
			if ( string.sub( text, 2, 1 + string.len( command ) ) == command ) then
				ply:ConCommand( "connect " .. address )
				return true
			end
		end
	end
end)

I am posting the code in the addons/ulx/lua/ulx/modules/sh folder

Honestly I don’t know, I just copy pasted your code into autorun/client/, ran it on a server, and said !ttt and it worked.

[img_thumb]http://i.imgur.com/yiLeinO.png[/img_thumb]

It seems to only happen when I type !ttt. But it works when I type !TTT