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

What does this script do?

Asked by 10 years ago
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?

0
How could I implement this so it makes the gui text fade away? TheAsianGamerHD 5 — 10y

3 answers

Log in to vote
2
Answered by 10 years ago

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.

0
Quick question, wouldn't you need a longer wait in the loop, so that it doesn't make the transparency jump quickly to 1? Spongocardo 1991 — 10y
0
You are right, thank you for correcting that. damagex443 325 — 10y
0
No problem, just a random thought I had when looking at the code. Spongocardo 1991 — 10y
Ad
Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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
Log in to vote
0
Answered by
hudzell 238 Moderation Voter
10 years ago

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.

Answer this question