Help with ReserveServer

I need help with reserving a server, I’m using this

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

local dataStore = DataStoreService:GetGlobalDataStore()

local code = dataStore:GetAsync("ReservedServer")
if typeof(code) ~= "string" then -- None saved, create one
	code = TeleportService:ReserveServer(game.PlaceId)
	dataStore:SetAsync("ReservedServer", code)
end

local function joined(player)
	player.Chatted:Connect(function(message)
		if message == "reserved" then
			TeleportService:TeleportToPrivateServer(game.PlaceId, code, { player })
		end
	end)
end

Players.PlayerAdded:Connect(joined)

Which does teleport the player to another server however every player is teleported to the same server… I was wondering how I could make it give them each there own server?

1 Like

Generate a private server code for each player.

1 Like

Oh i see, how would I do that?

1 Like

Generate it inside the joined function.

1 Like

…and also set the key for the datastore to be unique

1 Like

How would I go about making the key unique? I was thinking of just using the players PlayerID, would that work?

Yep! You might want to also make it like:

dataStore:SetAsync("ReservedServer"..player.UserId, code)