Invalid list of players for teleport

I have been trying to solve this for hours and I need to get this solved. This script is suppose to teleport you to the private server. I have tested this in the game and it gives this error:


If you can solve this issue you saved me hours of my life.

local TS = game:GetService("TeleportService")
local Players = game:GetService("Players")
local code = TS:ReserveServer(86819814841501)
local players = Players:GetPlayers()
local badgeID = 155440073325875
local part = script.Parent

local debounce = true


part.Touched:Connect(function(hitpart)
	local humanoid = hitpart.Parent:FindFirstChild("Humanoid")
	if humanoid then
		debounce = false
		for i, player in pairs(game.Players:GetPlayers()) do
			player.PlayerGui.LoadingScreen.Frame.Visible = true
			game:GetService("BadgeService"):AwardBadge(player.UserId, badgeID)
			wait(4)
			TS:TeleportToPrivateServer(86819814841501, code, players)
		end
	end
end)

Players:GetPlayers

“This method returns a table of all presently connected Player objects. It functions the same way Instance:GetChildren() would except that it only returns Player objects found under Players. When used with a for loop, it is useful for iterating over all players in a game.”

The key takeaway from this function’s API reference is “presently connected”. Your script treats players as if it’s an ever-changing and up-to-date list of all in-game players, but this is not correct. Assuming this script is in Workspace, your script will run as soon as the game server boots up. This means it has the chance to begin executing before any clients connect to that game server. Your script collects the presently connected players only once, and never does this again. It is likely that you requested that data too early and gave TeleportToPrivateServer an empty array. To get the most up-do-date array of the presently connected players, call Players:GetPlayers within your event listener:

local players = Players:GetPlayers()

for _, player in players do
    -- Yada yada
end

TeleportService:TeleportToPrivateServer(..., players)
1 Like

I have replaced it and it doesnt show the gui anymore and doesnt teleport

Did you replace your code with mine verbatim? If so, that’s not what I intended you to do. Show me your current code

local TS = game:GetService("TeleportService")
local Players = game:GetService("Players")
local code = TS:ReserveServer(86819814841501)
local players = Players:GetPlayers()
local badgeID = 155440073325875
local part = script.Parent

local debounce = true


part.Touched:Connect(function(hitpart)
	local humanoid = hitpart.Parent:FindFirstChild("Humanoid")
	if humanoid then
		debounce = false
		for _, player in players do
			player.PlayerGui.LoadingScreen.Frame.Visible = true
			game:GetService("BadgeService"):AwardBadge(player.UserId, badgeID)
			wait(4)
			TS:TeleportToPrivateServer(86819814841501, players)
		end
	end
end)

There’s a very simple error in your code. Your variable players is getting all players at runtime - this is going to be nil since there are no players in the server when the game instance is first started. When your part touch event is triggered, it’s calling this nil value when you’re trying to teleport. Instead, you should call :GetPlayers() inside the touch event itself, so that it has an up-to-date array of players in the game at the time of the part being touched.

Another slight issue, you’re waiting 4 in a loop of every player to apply the badge, meaning some players will loose out on the badge as they’ll possibly get teleported away before it’s awarded. You also shouldn’t need the TeleportToPrivateServer function inside this loop.

Lastly, you have a debounce variable, but you actually never implement this! It should also be false by default as a common ground.

local TS = game:GetService("TeleportService")
local Players = game:GetService("Players")
local code = TS:ReserveServer(86819814841501)
local badgeID = 155440073325875
local part = script.Parent

local debounce = false


part.Touched:Connect(function(hitpart)
	local humanoid = hitpart.Parent:FindFirstChild("Humanoid")
	if humanoid and not debounce then
		debounce = true
		for i, player in pairs(Players:GetPlayers()) do
			player.PlayerGui.LoadingScreen.Frame.Visible = true
			game:GetService("BadgeService"):AwardBadge(player.UserId, badgeID)
		end
        wait(4)
		TS:TeleportToPrivateServer(86819814841501, code, Players:GetPlayers())
	end
end)

Hope this helps!

Dude tysm I have been looking for absolutely hours non stop for this

1 Like