Invalid list of players for teleport error

  1. What do you want to achieve?
    I want this script to give all the players in the server a badge and then teleport them to a private server

  2. What is the issue?
    It is giving this error

  3. What solutions have you tried so far?
    I have looked for 5 hours on devforum and nothing helped

This is the script:

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)

Not sure if this is related to the error, but your wait(4) and the teleport part should be after the loop, not within it.
Also, you are only defining the players thing at the very start, when you should probably be doing it within the .Touched event

It seems like the ‘players’ variable is not a valid list of players. And this is because the list is empty and has no players, because you use :GetPlayers() when the game initially starts which means no players will have been loaded in.

So you should declare the players variable after the part has been touched by a humanoid, and not when the game starts.

For example:

And I would also put the wait(4) and the code that teleports all the players outside of the for loop. (as @SeargentAUS suggested)
Also your debounce variable is not being read by the if statement.

part.Touched:Connect(function(hitpart)
	local humanoid = hitpart.Parent:FindFirstChild("Humanoid")
	if humanoid and debounce == true then
		debounce = false

        local players = Players:GetPlayers()

		for i, player in pairs(game.Players:GetPlayers()) do
			player.PlayerGui.LoadingScreen.Frame.Visible = true
			game:GetService("BadgeService"):AwardBadge(player.UserId, badgeID)
		end

		wait(4)
		TS:TeleportToPrivateServer(86819814841501, code, players)
	end
end)

Hope this makes sense, let me know if you have any other questions.