Teleport to Private Server Not Working

Hello! So I’m trying to make a teleport thing, pretty simple, but it won’t really work.

Every time I try to teleport, it says “TeleportToPrivateServer must be passed an array of players”

I looked at the official ROBLOX TeleportToPrivateServer topic thing and that didn’t work either, so this is my only option (the dev forum).

Notes:
The script is in a server script.
Everything else works except the teleport part.

Please help!

wait(3)

local parent = script.Parent

local teleportService = game:GetService("TeleportService")
local teleportID = 7682902943
local transitionEvent = game.ReplicatedStorage.TransitionEvent
local noKeyEvent = game.ReplicatedStorage.NoKeyEvent
local teleporting = false

local players = game:GetService("Players")

local playersToTeleport = players:GetPlayers()

local debounce = true
local cooldown = 1000

local function teleportPlayers()
	local code = teleportService:ReserveServer(teleportID)
	teleporting = true
	teleportService:TeleportToPrivateServer(teleportID, code, playersToTeleport)
end

parent.Touched:Connect(function(hit)
	local character = hit.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	if hit.Parent:FindFirstChild("Humanoid") and debounce == true then
		if hit.Parent:FindFirstChild("Key1") or player.Backpack:FindFirstChild("Key1") then
			debounce = false
			transitionEvent:FireAllClients()
			wait(10)
			teleportPlayers()
			
			wait(cooldown)
			debounce = true
		else
			noKeyEvent:FireAllClients()
		end
	end
end)
1 Like

I think your issue might be coming from the fact that you are getting all the players before a player could even be joined. You aren’t updating the table or anything. I recommend moving playersToTeleport into the teleportPlayers() function:

local function teleportPlayers()
        local playersToTeleport = players:GetPlayers()
	local code = teleportService:ReserveServer(teleportID)
	teleporting = true
	teleportService:TeleportToPrivateServer(teleportID, code, playersToTeleport)
end

Hope this helps!

Oh! I see, I’ll try this out.Thank you very much!

It worked! Thank you so much!!

2 Likes

Alternatively, you could use the following (which I think will work better for your use case):

local players = game:GetService("Players")
local playersToTeleport

player.PlayerAdded:Connect(function(player)
	playersToTeleport = players:GetPlayers()
end)

Here whenever the PlayerAdded event is fired (when a player joins) the connected function is executed and the list of players is updated.