Help with reserving private servers

Hi! I’m trying to make a platform within the lobby of a game that teleports everyone into a reserved private server after either 20 players are on the platform or 90 seconds. I don’t really have a good understanding of how to use this service, so I think I did something wrong.

local TeleportService = game:GetService("TeleportService")

local function teleportPlayers(players)
	local code = TeleportService:ReserveServer(6064857231)
	TeleportService:TeleportToPrivateServer(6064857231, code, players)
end

local playersOnPlatform = {}
local part = workspace.TeleportHandle

part.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		table.insert(playersOnPlatform, player)
		game.ReplicatedStorage.Platform:FireClient(player, "On")
	end
end)

part.TouchEnded:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		table.remove(playersOnPlatform, table.find(playersOnPlatform, player))
		game.ReplicatedStorage.Platform:FireClient(player, "Off")
	end
end)

local g = 90
while #playersOnPlatform >= 8 and #playersOnPlatform < 20 and g > 0 do
	wait(1)
	g -= 1
end

if g == 0 or #playersOnPlatform == 20 then
	teleportPlayers(playersOnPlatform)
end

When I modified it to teleport after 1 player touched the platform and went into the regular game server (not studio test mode), it didn’t teleport me in anywhere. However, the remote events did end up firing, so I know it’s probably an issue with the teleporting part of the script. I’m not really sure how to fix this because I’m not great at teleport service. Can someone help me figure this out?

teleportation function is fine, i’m thinking players is returning as nil, or

isn’t firing. do some debugging with print statements so we can pinpoint which part of code isnt running etc.

oh, it’s probably only firing once right when the game is loaded because the repeat wait function isn’t waiting. let me see if I can make it fire starting with the touched command instead.

okay I think I figured out the issues.

  1. in the initial function, the loop to fire the teleportation only happened once. to fix this, I added a while true do loop. this is what my code looks like now:
while true do
	local g = 60
	while #playersOnPlatform >= 8 and #playersOnPlatform < 20 and g > 0 do
		wait(1)
		g -= 1
	end

	if g == 0 or #playersOnPlatform == 20 then
		teleportPlayers(playersOnPlatform)
	end
	wait()
end
  1. I forgot to add a debounce to my function. I’m pretty sure the reason the original function didn’t work after fixing the loop is because the lack of a debounce added the player to the table so many times that it was outside the counting range. to fix this, I just added a check to make sure the player wasn’t already in the table.
part.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not table.find(playersOnPlatform, player) then
			table.insert(playersOnPlatform, player)
			game.ReplicatedStorage.Platform:FireClient(player, "On")
		end
	end
end)

this is the final code with all edits:

local TeleportService = game:GetService("TeleportService")

local function teleportPlayers(players)
	local code = TeleportService:ReserveServer(6064857231)
	TeleportService:TeleportToPrivateServer(6064857231, code, players)
end

local playersOnPlatform = {}
local part = workspace.TeleportHandle

part.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not table.find(playersOnPlatform, player) then
			table.insert(playersOnPlatform, player)
			game.ReplicatedStorage.Platform:FireClient(player, "On")
		end
	end
end)

part.TouchEnded:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		table.remove(playersOnPlatform, table.find(playersOnPlatform, player))
		game.ReplicatedStorage.Platform:FireClient(player, "Off")
	end
end)

while true do
	local g = 60
	while #playersOnPlatform >= 8 and #playersOnPlatform < 20 and g > 0 do
		wait(1)
		g -= 1
	end

	if g == 0 or #playersOnPlatform == 20 then
		teleportPlayers(playersOnPlatform)
	end
	wait()
end
1 Like