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

For Loops & Transparency

Asked by
RSoMKC 45
11 years ago

Pretty simple question: How do I make the Parent of this script (Which is a transparent Part) turn opaque with steps of 0.1 and a speed of 0.1 seconds.

1for i=1,0, -0.1 do
2    script.Parent.Transparency = (i)
3end

4 answers

Log in to vote
4
Answered by
Unclear 1776 Moderation Voter
11 years ago

Your script works fine, the only problem is you aren't waiting! ROBLOX has this nice function named wait which halts the script for the amount of seconds input into the function. This is useful for pauses of all kinds, including the one you want!

1for i=1,0, -0.1 do
2    wait(0.1)
3    script.Parent.Transparency = i
4end
0
Note that because this will loop 10 times, it will result in a total time of 1 second. Bubby4j 231 — 11y
0
How do I make it so the loop only runs until the Parent is opaque? So it won't turn transparent again, that is. RSoMKC 45 — 11y
1
Use a while loop instead of a for loop. Set the condition to a check to see whether or not the part has reached its desired transparency. Unclear 1776 — 11y
Ad
Log in to vote
0
Answered by 11 years ago
1for i=1, 10 do
2wait(.1)
3script.Parent.Transparency = script.Parent.Transparency + 0.1
4end

This will add 10% transparency to the part every 1/10th of a second

Log in to vote
-1
Answered by
Trewier 146
11 years ago

Simple:

1for i=1,0, -0.1 do
2    script.Parent.Transparency = (i)
3    wait(.1)
4end

Or if you mean .1 seconds for the whole thing:

1for i=1,0, -0.1 do
2    script.Parent.Transparency = (i)
3    wait(0.01)
4end
0
Wasn't aware that the wait() was needed over there, thanks! Now that I'm looking at the loop a bit better I feel stupid. RSoMKC 45 — 11y
Log in to vote
-1
Answered by
Bubby4j 231 Moderation Voter
11 years ago

You can calculate the time needed to wait like this. I included nice variables so you can change it up easily.

1local speed = 0.1 --Time in seconds
2local stepsize = 0.1 --Step size
3for i = 1, 0, -1*stepsize do
4    script.Parent.Transparency = i
5    wait(speed*stepsize)
6end

If you want a function, here you go:

01function animateTransparency(part, seconds, stepsize, endTransparency)
02    while part.Transparency != endTransparency do
03        if part.Transparency < endTransparency  then
04            if part.Transparency + stepsize > endTransparency then
05                part.Transparency =  endTransparency
06            else
07                part.Transparency = part.Transparency + stepsize
08            end
09        else
10            if part.Transparency - stepsize < endTransparency then
11                part.Transparency =  endTransparency
12            else
13                part.Transparency = part.Transparency - stepsize
14            end
15        end
View all 21 lines...

Answer this question