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

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

local children = script.Parent.Parent:GetChildren()
for i = 1, #children do
	if children[i]:IsA("BasePart") then

-----should a break go here-------
		print("script ran")
-----or here?---------

	end
end

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

✓ Accepted answer

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.

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

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

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