Redirecting a server

Was curious as to how anyone on FP has gone about redirecting one of their servers to another, for example if switching dedi’s. I tried to hook onto InitPostEntity and all that with a SendLua to run “connect”, “ip” as a console command but it just crashes the player. Any and all help will be appreciated.

SendLua it on PlayerInitialSpawn.



local ip = "0.0.0.0:27015"
local password = ""

local function redirect()
    BroadcastLua("LocalPlayer:ConCommand('password " + password + "')")
    BroadcastLua("LocalPlayer:ConCommand('connect " + ip + "')")
end
hook.Add("PlayerInitialSpawn","RedirectPlayer",redirect)


Untested.

Why would you broadcast it if all existing players are already redirected?



local ip = "0.0.0.0:27015"
local password = ""

local function redirect(ply)
    ply:ConCommand('password ' + password)
    ply:ConCommand('connect ' + ip)
end
hook.Add("PlayerInitialSpawn","RedirectPlayer",redirect)


Happy?

IIRC you can’t use connect as ConCommands as server. That’s blocked.
so instead you should use:

[lua]local ip = “0.0.0.0:27015”
local password = “”
if (SERVER) then
util.AddNetworkString(“redirect”)

local function redirect(ply)
net.Start(“redirect”)
net.Send(ply)
end
hook.Add(“PlayerInitialSpawn”,“RedirectPlayer”,redirect)

else
net.Receive( “redirect”, function( len )
LocalPlayer():ConCommand('password ’ + password)
LocalPlayer():ConCommand('connect ’ + ip)
end )
end
[/lua]

Can you add like if isusergroup (admin or mod) no redirection?

I really fucking love this method of checking things. I’m sure you can work it into the code above yourself:




local userstable = {
"admin",
"owner",
"superadmin"
}

if not table.HasValue(userstable, ply:GetUserGroup()) then
--code here
end



pls niandra no
u should know better pugger
[lua]
– true = no redirect
local usergroups = {
admin = true;
owner = true;
founder = true;
superadminge = true;
}

if(not usergroups[ply:GetUserGroup()]) then
redirect()
end
[/lua]

Oh neato, I keep forgetting to make use of with tables

I’m receiving an attempt to perform arithmetic on a stringvalue when I use your code Tomelyr

ahh damn copy paste on my side…
all swadicalrag fault.

[lua]local ip = “0.0.0.0:27015”
local password = “”
if (SERVER) then
util.AddNetworkString(“redirect”)

local function redirect(ply)
net.Start(“redirect”)
net.Send(ply)
end
hook.Add(“PlayerInitialSpawn”,“RedirectPlayer”,redirect)

else
net.Receive( “redirect”, function( len )
LocalPlayer():ConCommand('password ’ … password)
LocalPlayer():ConCommand('connect ’ … ip)
end )
end[/lua]

Or clientside only?
[lua]
hook.Add(‘Think’, ‘REDIRECTPLAYER’, function()
if IsValid(LocalPlayer()) then
LocalPlayer():ConCommand(‘connect x.x.x.x’)
end
end)
[/lua]

I once asked my server coder to make a redirect addon that told the user the current state of things when joining and let them choose whether to redirect to the new server or exit upon actually joining, and there was no way to close the window. If he still has it and is fine with me linking it, I’ll let you use it. you could probably modify it to just redirect the person easily enough, too.



local ip = "0.0.0.0:27015"
local password = ""
local usergroups = {
  superadmin = true;
  admin = true;
  moderator = true;
}
if(not usergroups[ply:GetUserGroup()]) then
  redirect()
end
if (SERVER) then
util.AddNetworkString("redirect")

local function redirect(ply)
     net.Start("redirect")
     net.Send(ply)
end
hook.Add("PlayerInitialSpawn","RedirectPlayer",redirect)

else
 net.Receive( "redirect", function( len )
    LocalPlayer():ConCommand('password ' .. password)
    LocalPlayer():ConCommand('connect ' .. ip)
 end ) 
end


Is this fine?

[lua]
local ip = “0.0.0.0:27015”
local password = “”
local usergroups = {
superadmin = true;
admin = true;
moderator = true;
}

if (SERVER) then
util.AddNetworkString(“redirect”)

local function redirect(ply)
if(usergroups[ply:GetUserGroup()]) then return end
net.Start(“redirect”)
net.Send(ply)
end
hook.Add(“PlayerInitialSpawn”,“RedirectPlayer”,redirect)

else
net.Receive( “redirect”, function( len )
LocalPlayer():ConCommand('password ’ … password)
LocalPlayer():ConCommand('connect ’ … ip)
end )
end
[/lua]

you tried to run an function with redirect(), but the function is inside the net.Receive.

Tomelyr, your code gives off an error along the lines of Attempting to call method ConCommand.

For now ms I’ve taken your suggestion and done it all clientside on the think hook but my only concern is that it spams console because there is no LocalPlayer whenever nobody’s on the server. It isn’t that annoying, just a note.

WitheredPyre, if you don’t mind, I’d appreciate that a lot. I’ve been a little busy with real life things and servers on other games that I’m just trying to get this redirect to work

EDIT: It seems as if with the clientside hook on think, some players crash on join and some don’t. This doesn’t always happen tho. Will look into it more