for i=1,20 do wait() TextLabel.TextTransparency = TextLabel.TextTransparency + 0.05 end
What I don't understand is what these "i=1, 20" actually mean, can someone direct me to a tutorial on how to use these in scripts?
You're using a For Loop
the first parameter i = 1
tells the loop to start iterating from 1 and the second parameter, 20 in your code, means that it runs to 20. There is also a third parameter where you can set how many steps it will do each time it runs the loop.
Another example of what can be done with a for loop:
for i=0, 1, 0.05 do -- This will run from 0 to 1 and adds 0.05 every time the loop finishes wait(0.1) TextLabel.TextTransparency = i -- Set the Transparency to the current i value end
Look at http://wiki.roblox.com/index.php?title=Loops for more information.
The code you shared is actually not "good code". It would be much better to do this:
for i = 0, 1, 0.05 do wait() TextLabel.TextTransparency = i end
This is called a for
loop. Loops repeat code. Basically, the above loop is equivalent to this:
TextLabel.TextTransparency = 0 TextLabel.TextTransparency = 0.05 TextLabel.TextTransparency = 0.1 TextLabel.TextTransparency = 0.15 TextLabel.TextTransparency = 0.2 ... TextLabel.TextTransparency = 0.9 TextLabel.TextTransparency = 0.95 TextLabel.TextTransparency = 1
That's a lot of copying and pasting, though. You're likely to make mistakes, and if you ever decided you want to do something just slightly different, you have to start all over.
Hypothetically, maybe we actually wanted the transparency to fade slowly at first but quickly become invisible. We could do this by cubing the values (e.g., 0.3
becomes 0.027
, 0.9
becomes .729
)
We could write this explicitly:
TextLabel.TextTransparency = 0 TextLabel.TextTransparency = 0.000125 TextLabel.TextTransparency = 0.001 TextLabel.TextTransparency = 0.003375 ...
This is even worse! One option to make it perhaps a little clearer is to use variables:
i = 0 TextLabel.TextTransparency = i ^ 3 i = 0.05 TextLabel.TextTransparency = i ^ 3 i = 0.1 TextLabel.TextTransparency = i ^ 3
What's nice is that we can really easily see the formula (i ^ 3
) and that line is repeated over and over -- less slight tweaking.
Of course, we have i = 0
and i = 0.55
interrupting all over. This is where the for
loop comes in.
The for
loop essentially inserts the assignments to i
for us.
for i = 0, 1, 0.05 do
This means "do the following code, but for each value of i
from 0 to 1 in steps of 0.05"
Here's a simple example to run to also help get an intuition:
for variable = 1, 5 do print( "variable = ", variable) print( variable * variable ) end
The i=1,20 is this: i = the variable, in this script's case, it's not used or needed. The 1 means how many times it repeats the script before moving on to the next repeat, which is the 20. The 20 tells the for loop to repeat it 20 times. So putting a script inside of this for loop basically tells the code to repeat it's self 20 times.