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

how do you make a while loop change numbers?

Asked by 9 years ago

im searched on google and youtube i cant find any answers so im asking here, how would you make a loop always change the numbers off something, like the number always going down, if the number is like 30 and i want it to go down by one and one?

1 answer

Log in to vote
2
Answered by
IXLKIDDO 110
9 years ago

To put it simple, you would want to change the value every time the while loop is executed. For example (on mobile, so you should tab it out, as I cannot):

local i = 30
print(i)
while i > 0 do
i = i - 1
print(i)
end

The while loop is a loop that simply checks if the value is true or not. So the while loop will make it print the following:

30 -- This prints out before the loop is executed. If it wasn't then the script would print out the following numbers twice (except 0).
29
28
27
-- Skipping ahead
3
2
1
0

The reason why it goes down to zero is because if "i" is still 1, the loop will continue. This is due to the fact that 1 is more than zero. The reason why it isn't more than or equal to, is because you would then get a -1 output as well.

You could input this code anywhere you'd need to. For example a function can be used and the value of "i" can be changed.

0
thanks! this hleped alot!! User#8621 0 — 9y
Ad

Answer this question