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)```
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
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```
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
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