About Loops
A loop is a statement, or chunk of code, that is executed multiple times.
In Lua, there are three types of loops: for, while, and repeat loops.
The while loop will continue executing its body as long as its condition evaluates to true.
The repeat loop is similar to the while loop in that it will continue to execute its body for as long as its condition is false. It is, in a sense, a reversed while loop. Unlike a while loop, the condition check will also occur at the end of a loop step, meaning a repeat loop's body will always execute at least once.
The last kind is the for loop. There are actually two different sub-kinds of for loops:
- Numeric
for loops; and
- Generic
for loops.
Today we will only see one of them, the numeric for loop.
The Numeric for Loop
A numeric for loop is like a while loop that keeps track of a counter variable.
As long as the value of that counter variable is below a fixed limit, the loop continues.
It requires two arguments and can take an optional third argument:
- The first argument is the initial value of the counter;
- The second argument is the counter limit; and
- The third argument is the "step" of the counter variable.
The step determines by how much the counter variable will increase (or decrease) after each step of the loop. If not given then the step argument will take the value 1 by default.
Here is the syntax for a numeric for loop:
for counter = init, limit, step do
end
And here is a simple example:
for i = 1, 99 do
print(i)
end
About Your Problem
Your current loop starts from 0 and counts up to 1
in increments (or steps) of 0.1.
for i = 0, 1, 0.1 do
game.Workspace.Part.Transparency = 1 - i
wait(0.1)
end
If you want to reverse its behavior, then you have
two solutions.
First, you can count down from 1 to 0 in steps of -0.1...
for i = 1, 0, -0.1 do
game.Workspace.Part.Transparency = 1 - i
wait(0.1)
end
Or alternatively, you can keep counting up, but instead subtract the counter's value from the limit and use the difference.
for i = 0, 1, 0.1 do
game.Workspace.Part.Transparency = 1 - i
wait(0.1)
end
Hope that helps!
1 comment
for i = 1, 0, -0.1