Unable to teleport to a private server

Hello there,

I am trying to get players that are already in a private server to teleport to another private server that is reserved for the server owner everything I try it keeps teleporting me to a public server.

local HttpService = game:GetService("HttpService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeleportService = game:GetService("TeleportService")
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

local moduleFolder = game:GetService("ServerScriptService"):WaitForChild("Modules"):WaitForChild("Services")

local serverDataStore = DataStoreService:GetDataStore("PrivateServerData")

local placeId = 11522236915

local requestQueue = {}
local maxConcurrentRequests = 5
local currentRequests = 0

local function processQueue()
	while #requestQueue > 0 and currentRequests < maxConcurrentRequests do
		currentRequests = currentRequests + 1
		local request = table.remove(requestQueue, 1)
		request()
		task.wait(1)
		currentRequests = currentRequests - 1
	end
end

local function enqueueRequest(func)
	table.insert(requestQueue, func)
	task.defer(processQueue)
end

local function getDataWithRetry(dataStore, key, maxRetries)
	local retries = 0
	local success, result

	repeat
		success, result = pcall(function()
			return dataStore:GetAsync(key)
		end)

		if not success then
			retries = retries + 1
			warn("Retrying GetAsync for key: " .. key .. " (Attempt " .. retries .. ")")
			task.wait(2 ^ retries)
		end
	until success or retries >= maxRetries

	if not success then
		warn("Failed to retrieve data after " .. retries .. " attempts.")
	end

	return success, result
end

local function getKeyForPlayer(player)
	if not player or not player.UserId then
		warn("Invalid player object or UserId is nil.")
		return nil
	end

	local success, serverData = getDataWithRetry(serverDataStore, "Player_" .. player.UserId, 3)
	if success and serverData then
		return serverData.PrivateId
	else
		return nil
	end
end

local ownerUserId = game.PrivateServerOwnerId
local ownerAccessCode

local function createReservedServerForPlayer(player)
	local reserveSuccess, codeOrError = pcall(function()
		return TeleportService:ReserveServer(placeId)
	end)

	if not reserveSuccess then
		warn("Failed to reserve server: " .. tostring(codeOrError))
		return nil
	end

	local code = codeOrError

	local saveSuccess, errorMessage = pcall(function()
		serverDataStore:SetAsync("Player_" .. player.UserId, { PrivateId = code, PlayerId = player.UserId })
	end)
	if not saveSuccess then
		warn("Failed to save new reserved server data: " .. tostring(errorMessage))
		return nil
	end

	if player.UserId == ownerUserId then
		ownerAccessCode = code
	end

	return code
end

local function initializeOwnerAccessCode()
	local ownerPlayer = Players:GetPlayerByUserId(ownerUserId)
	if ownerPlayer then
		ownerAccessCode = getKeyForPlayer(ownerPlayer)
	end
	if not ownerAccessCode then
		ownerAccessCode = createReservedServerForPlayer(ownerPlayer)
	end
	if not ownerAccessCode then
		warn("Failed to initialize owner's access code.")
	end
end

local function teleportPlayerToReservedServer(player)
	initializeOwnerAccessCode()
	if ownerAccessCode then
		local teleportSuccess, errorMessage = pcall(function()
			print("Teleporting player: " .. player.Name .. " to owner's server with access code: " .. ownerAccessCode)
			TeleportService:TeleportToPrivateServer(placeId, ownerAccessCode, { player })
		end)
		if not teleportSuccess then
			warn("Failed to teleport player to the private server: " .. tostring(errorMessage))
		end
	else
		warn("Owner's access code is not available.")
	end
end

ReplicatedStorage.Events.TeleportToPrivate.OnServerEvent:Connect(function(player)
	task.wait(2)
	local UI = player:FindFirstChild("PlayerGui") and player.PlayerGui:FindFirstChild("Redirecting", true)
	if not UI then
		warn("Redirecting UI not found for player: " .. player.Name)
		return
	end

	UI.Enabled = true
	enqueueRequest(function()
		teleportPlayerToReservedServer(player)
	end)
end)

If anyone knows what I’m doing wrong please let me know.

Cheers
Sim

Still looking for solutions if any!

A reserved server is different from a private server, you could set the reserved server to not let people jist join and send the place id to any players that have permission to join or friends.

You can pass info on tp to let the server know what kind of server it is, and messaginfservice is a good way of sending info across servers for this

1 Like

I appreciate the help. I think I’ll try using the teleportData and modifying a value on the other server with the PrivateServerOwnerId so that the other server knows who owns it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.