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

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

Asked by
igric 29
6 years ago

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

Log in to vote
1
Answered by
RayCurse 1518 Moderation Voter
6 years ago
Edited 6 years ago

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.

Ad
Log in to vote
0
Answered by 6 years ago

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

Answer this question