Which loop is broken?

while wait() do
   for i = 1, 10 do
      wait(1)
      break
   end
end

Which loop is broken? The for loop, the while loop, or both? I assume the for loop, correct?

Also, if I had the same code, but the break was inside an if statement or something, would it still work?

while wait() do
   for i = 1, 10 do
      wait(1)
      if blank then
         break
      end
   end
end
1 Like

The for loop would stop (aka break) firstly since the break is in its scope.

1 Like

Ok, that’s what I had thought- and if it’s in an if statement, it still works too?
So, the break will always end the loop it is inside of, I guess?

Yep, would still work if you nested it in several other if statements. There might be a maximum (75% chance not maybe)??

75% chance not
Like
You’re not sure if it will or not?

Noo, it will break but I’m not sure if there’s a maximum to how much you can nest it down.

The while loop is broken, wait() is not an acceptable iterator.

Wait what
Can you elaborate…?

word limitttttttttttttttt

Try doing something like:

if wait() then
    for i = 1,10 do
        wait(1)
    end
end

Please mark this as a solution and leave a like if it helped!

Are you saying that “while wait() do” doesn’t work or it doesn’t work in my scenario

What are you saying here? Why would while wait() do not work?

The if statement checks whether or not a value or statement is true or false, and if it is then code happens. if wait() then won’t work as intended because it’s checking the function that’s been passed, which in this situation would yield and then return it’s value to where the code can then continue. It won’t infinitely loop.

@iiCant_Read’s program does what it’s supposed to.

oh thank god i thought i was going insane

There isn’t a maximum, break & continue will break/continue respectively their immediate next loops they are contained within.

while task.wait() do
	if true then
		if true then
			if true then
				if true then
					if true then
						break
					end
				end
			end
		end
	end
end

print("Hello!")

“Hello!” is still printed to the console as the containing while loop is broken out of once the break statement is executed.

1 Like

The top one is broken because the break is inside the loop, and the bottom one is breaking likely because of the if statement also within the loop.

I believe they’re asking about how the break function interacts with nested loop, not about whether the code will work.