Teleporter only teleporting 1 player

Hello. I am trying to make a round system. So far I need to tp the players to a part.
The problem is, that it only teleports 1 person to the part.
I couldn’t find any videos on helping this. If anyone knows why its only teleporting 1 player then please help. I want to tp the whole server.

	if inRound.Value == true then
		for _, player in pairs(game.Players:GetChildren()) do
			
			player.Character.HumanoidRootPart.CFrame = CFrame.new(mapTp.Position)
			playing() --This is a function for my code when it starts the round.
		end
	else
		for _, player in pairs(game.Players:GetChildren()) do
			
			player.Character.HumanoidRootPart.CFrame = CFrame.new(lobbyTp.Position)
			intermission() --This is a function for my code when it ends the round.
		end
	end
	
end)```
3 Likes

It’s because of the current loop running. The current loop waits for the function to finish which is why it will only move to the next loop if the function is finished. Use task.spawn to solve it by creating a new “thread” for the function to run on.

New code:

if inRound.Value then
	for _, player in pairs(game.Players:GetPlayers()) do
		if not player.Character then
			continue
		end

		player.Character:PivotTo(CFrame.new(mapTp.Position))
		
		task.spawn(function()
			playing() --This is a function for my code when it ends the round.
		end)
	end
else
	for _, player in pairs(game.Players:GetPlayers()) do
		if not player.Character then
			continue
		end
		
		player.Character:PivotTo(CFrame.new(lobbyTp.Position))
		
		task.spawn(function()
			intermission() --This is a function for my code when it ends the round.
		end)
	end
end
1 Like

A few tips:

  1. Define Players as a variable at top of your script, such as local Players = game:GetService("Players").
  2. Use GetPlayers() instead of GetChildren(), this would make sure that Players’ children are actually Players.
  3. To move Players, use PivotTo(CFrame).

Now,
Do you mind showing those functions called in those loops?

The issue is that both the loop and function happen at the same time. Which we dont want, you can use either task.spawn or coroutines.

Here they are.

	print("Playing!")
	for i, v in pairs(Players:GetChildren()) do
		if v:IsA("Player") then
			v.Team = game.Teams.Playing
			wait(29.7)

			for i, plr in pairs(game.Players:GetChildren()) do

				local char = plr.Character
				local humanRoot = char:WaitForChild("HumanoidRootPart")
				if plr.Team == playingTeam then
					print("He/She won")
					plr.leaderstats.Wins.Value = plr.leaderstats.Wins.Value + 1
					plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 5
				else
					print("He/She lost.")
				end
			end
		end
	end
end

local function intermission(player)   --When the intermission starts run all commands in here
	print("Intermission!")
	for i, v in pairs(Players:GetChildren()) do
		if v:IsA("Player") then
			v.Team = game.Teams.Lobby
		end
	end
end```

Try my script, it should work with what you’re doing.

Yep I will. I just need someone to test it with me real quick. if you mind joining here is the link: Untitled Game - Roblox

Try this:

if inRound.Value then
	for _, player in pairs(game.Players:GetPlayers()) do
		if not player.Character then
			continue
		end

		player.Character:PivotTo(CFrame.new(mapTp.Position))
	end

	task.spawn(function()
		playing() --This is a function for my code when it ends the round.
	end)
else
	for _, player in pairs(game.Players:GetPlayers()) do
		if not player.Character then
			continue
		end

		player.Character:PivotTo(CFrame.new(lobbyTp.Position))
	end
	
	task.spawn(function()
		intermission() --This is a function for my code when it ends the round.
	end)
end

doesnt work bro. I dont know. I tried changing it to
if inRound.Value == true then
but it didnt work

Did the prints show when you added them?

Here they are.

	print("Playing!")
	for i, v in pairs(Players:GetChildren()) do
		if v:IsA("Player") then
			v.Team = game.Teams.Playing
			wait(29.7)

			for i, plr in pairs(game.Players:GetChildren()) do

				local char = plr.Character
				local humanRoot = char:WaitForChild("HumanoidRootPart")
				if plr.Team == playingTeam then
					print("He/She won")
					plr.leaderstats.Wins.Value = plr.leaderstats.Wins.Value + 1
					plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 5
				else
					print("He/She lost.")
				end
			end
		end
	end
end

local function intermission(player)   --When the intermission starts run all commands in here
	print("Intermission!")
	for i, v in pairs(Players:GetChildren()) do
		if v:IsA("Player") then
			v.Team = game.Teams.Lobby
		end
	end
end

it worked! Thanks so much for the help!