If statement in for loop not working [PLEASE HELP]

  1. What do you want to achieve?
    So I want to break the FOR loop when the IF statement is true.
  2. What is the issue?
    Even if the statement is true FOR loop is still going.

ReplicatedStorage

InRound = bool value.
PlayersAmountInMiniGame = int value.
Status = string value.

function roundTimer()
	while true do
		for i = intermissionLength, 1, -1 do
			InRound.Value = false
			Status.Value = "Intermission: "..i.." seconds left!"
			wait(1)
		end
		Status.Value = "Ready?"
		wait(1)
		for i = quickCounter, 1, -1 do
			InRound.Value = false
			Status.Value = i
			wait(1)
		end
		InRound.Value = true
		Status.Value = "GO!"
		wait(2)
		
		for i = roundLength, 1, -1 do
			InRound.Value = true
			Status.Value = "Game: "..i.." seconds left!"
			wait(1)
			if InRound.Value == true and playersAmountInMiniGame == 0  then
				break -- loop is still going, but it shouldn't
			end
			Status.Value = "Everyone DIED!"
		end
		Status.Value = "END!"
		wait(2)
	end
	Status.Value = "Everyone DIED!"
	InRound.Value = false
end

Are you sure you are counting the Players? Assuming its a table, it would be #playersAmountInMiniGame

It could also be the that the Previous loop hasn’t ended, or that the condition isn’t met, its very Likely playersAmountInMiniGame is returning false

Yeah, whenever player joins the minigame the value is changing.

I would Recommend putting them into a Table

Example:

AlivePlayers = {}

for number, player in game.Players:GetPlayers() do -- Gets Players
table.insert(AlivePlayers, player) -- Inserts Players

player.Character.Humanoid.Died:Connect(function() -- Connects a event when they die
table.insert(AlivePlayers, AlivePlayers[player]) -- Removes Player
end)
end

From there you can count how many:

If #AlivePlayers < 1 and InRound.Value == true then
-- code
end

Where should I put this code, @xGOA7x

AlivePlayers = {}

function roundTimer()
	while true do
		for i = intermissionLength, 1, -1 do
			InRound.Value = false
			Status.Value = "Intermission: "..i.." seconds left!"
			wait(1)
		end
for number, player in game.Players:GetPlayers() do -- Gets Players
table.insert(AlivePlayers, player) -- Inserts Players

player.Character.Humanoid.Died:Connect(function() -- Connects a event when they die
table.insert(AlivePlayers, AlivePlayers[player]) -- Removes Player
end)
end
		Status.Value = "Ready?"
		wait(1)
		for i = quickCounter, 1, -1 do
			InRound.Value = false
			Status.Value = i
			wait(1)
		end
		InRound.Value = true
		Status.Value = "GO!"
		wait(2)
		
		for i = roundLength, 1, -1 do
			InRound.Value = true
			Status.Value = "Game: "..i.." seconds left!"
			wait(1)
			if InRound.Value == true and #AlivePlayers < 1  then
				break -- loop is still going, but it shouldn't
			end
			Status.Value = "Everyone DIED!"
		end
		Status.Value = "END!"
		wait(2)
	end
	Status.Value = "Everyone DIED!"
	InRound.Value = false
end

Just Pasted them into the place i feel like who be best

Alright, I’ll try it and let you know if it’ll work.

Well, still nothing… maybe I should give you whole code, and you’ll see?

Hi. You’re missing your # with your intermissionLength! Try this:

for i = #intermissionLength, 1, -1 do

Is it a value? If so, try this then:

for i = intermissonLength.Value, 1, -1 do

Also, add [] to your “Game: “…i…” seconds left!”
Example:

Status.Value = "Game: "..[i].." seconds left!"

Also, your loop is still going because you’re breaking the for loop, not the while true do loop.

image
ehh

Try playersAmountInMiniGame.Value == 0? Also consider changing the name to playersLeft. And maybe <= 1 would be better because if there’s one player left, they win.

Sorry if somebody said this already, I didn’t read the replies yet.

I want to break the while loop. But I don’t know how.

If there is no code after the while loop, use return, otherwise make a variable above the for loop, and set it to true if you want to break the while loop. Use an if statement after the for loop checking this variable if it should break the while loop.

while true do
   local whilebreak = false
   for i = 1, 10 do
      if i * 2 == 4 then whilebreak = true break end
   end
   if whilebreak then break end
end

Hmm, I’ll show you my code, because while loop is still going.

function roundTimer()
	while true do
		local whileBreak = false
		for i = intermissionLength, 1, -1 do
			InRound.Value = false
			Status.Value = "Intermission: "..i.." seconds left!"
			wait(1)
		end
		Status.Value = "Ready?"
		wait(1)
		for i = quickCounter, 1, -1 do
			InRound.Value = false
			Status.Value = i
			wait(1)
		end
		InRound.Value = true
		Status.Value = "GO!"
		wait(2)

		for i = roundLength, 1, -1 do
			InRound.Value = true
			Status.Value = "Game: "..i.." seconds left!"
			wait(1)
			if InRound.Value == true and playersLeft.Value == 0  then
				whileBreak = true
				break
			end
			if whileBreak then
				break
			end
			Status.Value = "Everyone DIED!"
		end
		Status.Value = "END!"
		wait(2)
	end
	Status.Value = "Everyone DIED!"
	InRound.Value = false
end
1 Like

Also here are the Variables
image

Me:

		for i = roundLength, 1, -1 do
			InRound.Value = true
			Status.Value = "Game: "..i.." seconds left!"
			wait(1)
			if InRound.Value == true and playersAmountInMiniGame == 0  then
				i:Destroy()
			end

You:

		for i = roundLength, 1, -1 do
			InRound.Value = true
			Status.Value = "Game: "..i.." seconds left!"
			wait(1)
			if InRound.Value == true and playersAmountInMiniGame == 0  then
				break -- loop is still going, but it shouldn't
			end

instead do i:Destroy()