Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

should I put a break under or over an if statement inside a for loop?

Asked by 5 years ago
01local children = script.Parent.Parent:GetChildren()
02for i = 1, #children do
03    if children[i]:IsA("BasePart") then
04 
05-----should a break go here-------
06        print("script ran")
07-----or here?---------
08 
09    end
10end

I want the loop to break when children[i] is a basepart.

for the code under the for loop and the if statement, were is the best place to put a break.

1 answer

Log in to vote
1
Answered by
nc2r 117
5 years ago
Edited 5 years ago

Now that I understand the question, break goes after any code you want to run. Code executes top to bottom, so generally put break after any code you want to run.

1local children = script.Parent.Parent:GetChildren()
2for i = 1, #children do
3    if children[i]:IsA("BasePart") then
4        print("script ran")
5        break --here
6    end
7end

If you put break before the print, it would exit the loop first, so nothing would be printed.

Ad

Answer this question