How to teleport players from a table?

In my game I’m trying to make a platform where you touch a part and it’ll put you in a table and teleport anyone who is in the table to a map after a short countdown. However, when I test it out with two players, one player teleports. Can someone show me what’s the issue?

local players = game:GetService("Players")

local part = script.Parent

local touchedPlayers = {}

local function teleportPlayers()
	for i, v in pairs(touchedPlayers) do
		-- make copy of map from serverstorage and teleport players to the copy
		local Map1 = game.ServerStorage.Map1
		local Map1Copy = Map1:Clone()

		Map1Copy.Parent = workspace
		wait(1)
		v.Character.HumanoidRootPart.Position = Vector3.new(Map1Copy.Part.Position)
	end
end

local function gameCheck()
	if #touchedPlayers >= 1 then
		wait(5)
		teleportPlayers()
		print("start game")
	else
		print("not enough players")
	end
end

local debounce = false

part.Touched:Connect(function(hit)
	local hitModel = hit:FindFirstAncestorOfClass("Model")
	if hitModel then
		local hitPlayer = players:GetPlayerFromCharacter(hitModel)
		if hitPlayer and debounce == false then
			debounce = true
			game.ReplicatedStorage["TimerGUI"]:FireClient(hitPlayer) -- plays GUI timer
			if not table.find(touchedPlayers, hitPlayer) then
				table.insert(touchedPlayers, hitPlayer)
			gameCheck()
			end
		end
	end
	print(touchedPlayers)
end)

players.PlayerRemoving:Connect(function(player)
	local tablePlayer = table.find(touchedPlayers, player)
	if tablePlayer then
		table.remove(touchedPlayers, tablePlayer)
	end
end)
2 Likes

If you print the hitPlayer found in touchedPlayers in the teleportPlayers function do you get all the players you expect?

You realize you’re making a copy of the same map for each player right?

It seems that you forgot to set debounce back to false after you inserted the player into the table, thus preventing any more inserts.

How should I set debounce back to false? Here’s the code I changed:

local debounce = false

part.Touched:Connect(function(hit)
	local hitModel = hit:FindFirstAncestorOfClass("Model")
	if hitModel then
		local hitPlayer = players:GetPlayerFromCharacter(hitModel)
		if hitPlayer and debounce == false then
			debounce = true
			game.ReplicatedStorage["TimerGUI"]:FireClient(hitPlayer) -- plays GUI timer
			if not table.find(touchedPlayers, hitPlayer) then
				table.insert(touchedPlayers, hitPlayer)
			gameCheck()
			end
			if table.find(touchedPlayers, hitPlayer) then
				debounce = true
			else
				debounce = false
			end
		end
	end
	print(touchedPlayers)
	print(debounce)
end)

Like so:

if hitPlayer and debounce == false then
	debounce = true
	game.ReplicatedStorage["TimerGUI"]:FireClient(hitPlayer) -- plays GUI timer
	if not table.find(touchedPlayers, hitPlayer) then
		table.insert(touchedPlayers, hitPlayer)
		gameCheck()
                   
    end
    task.wait(1) -- cooldown if you want
    debounce = false
end