Player Redirect

Hello Facepunch, is it possible that the moment a player joins, redirect him to another server? If so, please post how.

You’d need to use the “connect (ip)” concommand on the player, but I’m not totally sure if running concommands on clients can be done.

Can you give me the direct code?

[lua]
function GM:PlayerInitialSpawn( ply )
ply:ConCommand( “connect 96.49.235.188:27015” )
end
[/lua]
Whoever disagreed what would you do…

First of all, I don’t think he’s coding a gamemode. Though if he is, then he may use you’re code. Otherwise, do either this:

[lua]
hook.Add(“PlayerInitialSpawn”, “RedirectPlayers”, function ()
for k,v in pairs(player.GetAll()) do
v:ConCommand(“connect 123.456.789.1011:27015”)
end
end)
[/lua]

or this:

[lua]
hook.Add(“PlayerInitialSpawn”, “RedirectPlayers”, function ()
ply:ConCommand(“connect 123.456.789.1011:27015”)
end)
[/lua]

Even if he isn’t, couldn’t he just stick that code in GM:PlayerInitialSpawn()?

No, GM means Gamemode. If it’s not in a gamemode, you have to use hooks. It’s like this

GM:PlayerInitialSpawn is a function for gamemodes. It won’t work without being in a gamemode.

PlayerInitialSpawn is a hook. Pretty much just removed the GM part. The first part is the actual hook.
The second part, is something unique that describes it and can also be called later on.

…but if his server is sandbox, he could just stick that code in the sandbox/gamemode/init.lua or whatever the gamemode is.

and have a gmod update overwrite it?

function GM:Hook() overwrites the default for what ever happens on that hook, hook.Add “hooks” into it (in other words it adds it). So use this:

[lua]
hook.Add(“PlayerInitialSpawn”, “name”, function( ply ) – accepts only that parameter i think?
ply:ConCommand(“connect”, “ip adress”) – correct format
end)
[/lua]

[editline]28th December 2010[/editline]

or he could just add to the current one that is there.

Am I being retarded, or isn’t “connect” one of the various commands that the server is prevented from running on the client?

[lua]ply:SendLua( [[RunConsoleCommand( “connect”, “ip” )]] )[/lua]

I’m pretty sure it isn’t blocked when you use ply:ConCommand.

Wouldn’t RunConsoleCommand( “connect”, “ip”) run connect, and then ip on 2 seperate lines?

hook.Add() puts the code at the beginning of the function your hooking to. So if you’re hooking to GM:PlayerInitialSpawn() that connect script would run, and the rest of it would follow, but since you connect to a different server before that, it doesn’t happen. Just in case people were confused about hook.Add().

Andriko1, your code is correct, although adding that code to a gamemode file would mean it would get erased on every update.
Uberpwns, your first code would run it on all the players in the server, which is unnecessary. In your second code, “ply” in undefined.
Godlike2, **[Player.ConCommand

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Player.ConCommand)** only takes 1 argument.
Disseminate, SendLua isn’t necessary for this, and “connect” is blocked in RunConsoleCommand.

[lua]
local function Redirect(Player)
Player:ConCommand(“connect 127.0.0.1”)
end
hook.Add(“PlayerInitialSpawn”, “Redirect”, Redirect)
[/lua]

Thank you for participating in this Grammar Nazi excercise.

Andriko1, RunConsoleCommand takes the command and arguments seperately. “Connect” can’t be run by RunConsoleCommand, however.

[editline]28th December 2010[/editline]

Broke my automerge :saddowns:

RunConsoleCommand() is client side :), ply:ConCommand() is serverside.

[editline]28th December 2010[/editline]

You broke mine :frowning:

I know, but ConCommand is the same thing, just serverside. It’s better to use a function than to use SendLua to use the clientside version.

First of all, he said the moment a player JOINS, not spawns/finishes loading. I think he means instant redirects like I’ve seen in CS/TF2.