Stopping a for loop mid loop

  1. What do I want to achieve?

I’m trying to stop the for loop if everyone is eliminated from the round. At the moment it bugs out and I do not know why.

  1. What is the issue?

The status bugs out and then the loop continues.

Lines 31-37 (It is labeled)

local statusText = game:WaitForChild("ReplicatedStorage").statusText
local repStorage = game:WaitForChild("ReplicatedStorage")
wait(#game.Players:GetPlayers()>=1)
while true do
	for number = 15,0,-1 do 
		wait(1)
		statusText.Value = "Intermission: " .. number
	end
	for _, v in ipairs(game.ServerStorage.barrierStorage:GetChildren())do
		v.Parent = workspace.barriers
	end
	statusText.Value = "Teleporting Players"
	a = game:GetService("Players"):GetPlayers()
	for _, v in ipairs(a) do
		v.Character.HumanoidRootPart.CFrame = CFrame.new(workspace.teleblock.CFrame.X,5,workspace.teleblock.CFrame.Z)
		v.Team = game:GetService("Teams").Playing
	end
	statusText.Value = "Teleporting Complete"
	wait(2)
	statusText.Value = "Game Starting"
	wait(2)
	statusText.Value = "Started"
	for _, v in ipairs(workspace.barriers:GetChildren())do
		v.Parent = game.ServerStorage.barrierStorage
	end
	for num = 5, 0, -1 do
		wait(1)
		statusText.Value = "Lava is rising in: " .. num
	end
	repStorage.startLavaRise.Value = true
	for num = 15, 0, -1 do                                                   BEGIN HERE
		wait(1)
		statusText.Value = "Time left: " .. num
		if #game.Teams.Playing:GetPlayers()== 0 then 
			break	
		end
	end                                                                                  END HERE
	repStorage.startLavaRise.Value = false
	for _, players in ipairs(game.Players:GetPlayers())do
		local rSpawn = workspace.spawnPads:GetChildren()[math.random(1, #workspace.spawnPads:GetChildren())]
		players.Character.HumanoidRootPart.CFrame = CFrame.new(rSpawn.CFrame.X, 5, rSpawn.CFrame.Z)
	end
	for i = 5, 0, -1 do 
		wait(1)
		statusText.Value = "Game Over: " .. i
	end
end

How exactly is it bugging out, please give some specifics!

The status text goes blank or says LABEL.

Correct me if I’m wrong but I don’t think “GetPlayers()” can be used here, try “GetChildren()” instead.

GetPlayers() is used under Game.Players to get the players in the game.

1 Like

You can actually use GetPlayers on a Team to get all the players on that team

I know, but it looks like they’re looking into something else that is inside of Team (I’m assuming a folder called “Playing” inside of Team), which is why I mentioned it, because if that’s the case then I’m still not sure if GetPlayers() would work here.

I could be completely wrong though since they might be moving the actual players in the folder, which should work in that case (I think?) so you can disregard my comment.

1 Like

“Playing” is most likely a Team object inside of the Teams service

2 Likes

I would make that loop, a function so that it can break from the nested loop because as you may have seen, using break in a nested loops breaks out of all the parent loops (if that makes sense?). So i would do something like:

local statusText = workspace.Part.SurfaceGui.TextLabel
local function Timer(From, To, By, Check, FuncEverySec)
	for num = From, To, By do
		wait(1)
		if FuncEverySec then
			FuncEverySec(num)
		end
		if Check and #Check:GetPlayers() then
			return
		end
	end
end
while true do
	Timer(15,0,-1, nil, function(CurrentTime)
		statusText.Text = "Intermission: ".. CurrentTime
	end)
	for _, v in ipairs(game.ServerStorage.barrierStorage:GetChildren())do
		v.Parent = workspace.barriers
	end
	statusText.Value = "Teleporting Players"
	a = game:GetService("Players"):GetPlayers()
	for _, v in ipairs(a) do
		v.Character.HumanoidRootPart.CFrame = CFrame.new(workspace.teleblock.CFrame.X,5,workspace.teleblock.CFrame.Z)
		v.Team = game:GetService("Teams").Playing
	end
	statusText.Value = "Teleporting Complete"
	wait(2)
	statusText.Value = "Game Starting"
	wait(2)
	statusText.Value = "Started"
	for _, v in ipairs(workspace.barriers:GetChildren())do
		v.Parent = game.ServerStorage.barrierStorage
	end
	Timer(5, 0, -1, nil, function(CurrentTime)
		statusText.Text = "Lava is rising in: ".. CurrentTime
	end)
	repStorage.startLavaRise.Value = true
	Timer(15, 0, -1, game.Teams.Playing, function(CurrentTime)
		statusText.Text = "Time left: " .. CurrentTime
	end)                                                                                
	repStorage.startLavaRise.Value = false
	for _, players in ipairs(game.Players:GetPlayers())do
		local rSpawn = workspace.spawnPads:GetChildren()[math.random(1, #workspace.spawnPads:GetChildren())]
		players.Character.HumanoidRootPart.CFrame = CFrame.new(rSpawn.CFrame.X, 5, rSpawn.CFrame.Z)
	end
	Timer(5, 0, -1, nil, function(CurrentTime)
		statusText.Text = "Game Over: ".. CurrentTime
	end)
end