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

How do I make loop stops after it reach a value I want?

For an example:

while true do
	x = x + 1
	if x == 20 then
	--how do I make it stop?  is there a special command??? nil?????
	end
end

2 answers

✓ Accepted answer

The while loop will only loop again if the boolean expression supplied by the scripter evaluates to true. The loop you provided in your question will run infinitely because true will always evaluate to true. You could replace the condition with one that is only true when x is below a certain limit such as the one below:

while x <= 20 do
	x = x + 1
end

x <= 20 will only evaluate to true when x is less than or equal to 20.

Just check if the value is what you want every time the loop runs, that would look like this:

If x==20 then break end

Make sure to put it in the loop

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