Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

i dont understand how does "break" work exactly, does it break all loops??

if i have 2 "for loops" inside each other, and i use "break" in the inner one does it exit from both loops or only the inner one?

1 answer

✓ Accepted answer

The break statement only breaks the innermost loop. So even in nested loops, it only breaks the loop the statement is inside of.

This example only breaks the while loop:

for i = 1, 10 do
    while true do
        print("hi\n------")
        break -- while loop broken out of
    end
    print(i)
end

And you can have as many nested loops as you want, only the innermost loop is broken.

Useful link

This discussion is preserved as it was. New answers, comments, and votes are closed.