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

Can you break out of a for loop, is that a possible thing to do?

For example, if I did this:

local test = game.Workspace.Test
for i = 1, 10 do
	if test.Touched then
		break
	end
end

Is that possible to do?

Or is break just for while loops? Just wanted to be sure whether you can do it or not, or if it's just not recommended to do it in your code?

4 comments

0

break works in all loops

0

Oh ok, wanted to make sure. Thanks!

0

I actually tested break out on a repeat loop on the command line to see if it worked, you could have easily done the same.

0

I know, I just thought about it :p It's just that this script of mine that doesn't seem to be breaking out of a for loop.

1 answer

✓ Accepted answer

Yes, you can use break in any loop, although I'm not sure you can do if part.Touched then in any script.

repeat print"this will only print once" break until nil

Because we break the loop here, it will only go once.

for q=1,10 do
	print(q) --this will print '1' and only '1'
	break
end
while true do
	print"once again, only prints once"
	break
end

break can be used in a plethora of ways, to achieve a variety of different results.

1 comment

0

Thank you so much for the information!

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