How to avoid this infinite loop on TeleportToPrivateServer

When a specific player enters the game, I want to TP him to a Private Server:

local TS = game:GetService("TeleportService")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	if player.Name == "rogeriodec_games" then
		local Key = TS:ReserveServer(game.PlaceId)
		TS:TeleportToPrivateServer(game.PlaceId, Key, {player})
	end
end)

However, it seems that every time TS:TeleportToPrivateServer is run, PlayerAdded is run again.
How to avoid this?

You can check game.PrivateServerId and if it’s not an empty string.

1 Like

You can check if the server is reserved by checking if the server doesn’t have a private server id and if the server doesn’t have any owner before connecting.

local TS = game:GetService("TeleportService")
local Players = game:GetService("Players")

if not (game.PrivateServerId ~= "" and game.PrivateServerOwnerId == 0) then
    Players.PlayerAdded:Connect(function(player)
	    if player.Name == "rogeriodec_games" then
		    local Key = TS:ReserveServer(game.PlaceId)
		    TS:TeleportToPrivateServer(game.PlaceId, Key, {player})
	    end
    end)
end
1 Like