Teleporting people to an already existing reserved server

I currently have some trouble figuring this out. Im trying to make an invasion system like in the souls games.
Currently how it works, is that when players join the hub place, they get teleported to the main place where the game takes place,

local TeleportService = game:GetService("TeleportService")
local MemoryStoreService = game:GetService("MemoryStoreService")

local serverRegistry = MemoryStoreService:GetSortedMap("ServerRegistry")

function createServer(player, region, avgLevel)
	local serverCode = TeleportService:ReserveServer(82655950777128)
	TeleportService:TeleportToPrivateServer(82655950777128, serverCode, {player})
end

game.Players.PlayerAdded:Connect(function(player)
	
	createServer(player, nil, 1)
	
end)

After that, in the main place, i use memory store service to add servers as entries:

function createServer(player, region, avgLevel)
	local serverCode = game.PrivateServerId

	if not serverCode or serverCode == "" then
		serverCode = "TestServer_" .. tostring(game.JobId)
		print("Using fallback server code:", serverCode)
	end

	local serverData = {
		code = serverCode,
		region = region or "global",
		avgLevel = avgLevel or 1,
		playerCount = 1
	}

	print("Saving data with key:", serverCode)
	print("Server Data:", serverData)

	local success, err = pcall(function()
		serverRegistry:SetAsync(serverCode, serverData, 100)
	end)

	if not success then
		warn("Failed to save server data:", err)
	end
end

When trying to join a different player though, i get an error: Attempted to teleport to a place that is restricted. (Error code: 773)

function findServer(region, playerLevel)
	local allEntries = serverRegistry:GetRangeAsync(0, 100)
	local currentServerCode = game.PrivateServerId

	for _, entry in ipairs(allEntries) do
		local serverData = entry.value
		local key = entry.key

		print("Checking server with key:", key, "Data:", serverData)

		if serverData.code and serverData.code ~= currentServerCode then
			print("Current server id:", game.PrivateServerId)
			print("Found suitable server:", serverData.code)
			return serverData.code
		end
	end

	print("No suitable servers found!")
	return nil
end

function joinServer(player, region, playerLevel)
	print("Attempting to invade player...")
	local serverCode = findServer(region, playerLevel)
	if serverCode then
		TeleportService:TeleportToPrivateServer(82655950777128, serverCode, {player})
	else
		
	end
end

This is the code for finding, and joining servers.


It does find the valid server code, but for some reason it cant join.
I need some help.

1 Like

In the first script you seem to be getting the actual code for the reservedserver, then in the second script where you save it to memory storage you use privateserverid as serverCode??

Are you sure the code you are getting isn’t just the privateserverid of the server the player wants to get teleported to because you need to use the code from the TeleportService:ReserveServer

(Also why don’t you have a variable for the placeid?)

1 Like

I thought it was the same thing ngl. I’ll try saving the same code i use for the reserveserver and ill get back to you. Ty!

1 Like

Thank you, that worked! Marked as solution.

1 Like

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