Hi,
So I know that this below script will make it Transparent. But, how would I make it go from Transparent back to 0. I dont know how to switch the numbers around. Thanks,
for i = 0, 1, 0.1 do game.Workspace.Part.Transparency = i -- Make the Transparency of the part equal to i. wait(0.1) end
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:
for
loops; andfor
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 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 -- Do something... end
And here is a simple example:
-- Count from 1 up to 100. 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!
You would need to do this:
for i = 1,0,-0.1 do game.Workspace.Part.Transparency = i wait(0.1) end
If this answer helped you, make sure you accept the answer! :-)