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
10 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.

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

4 answers

Log in to vote
4
Answered by
Unclear 1776 Moderation Voter
10 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!

for i=1,0, -0.1 do
    wait(0.1)
    script.Parent.Transparency = i
end
0
Note that because this will loop 10 times, it will result in a total time of 1 second. Bubby4j 231 — 10y
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 — 10y
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 — 10y
Ad
Log in to vote
0
Answered by 10 years ago
for i=1, 10 do
wait(.1)
script.Parent.Transparency = script.Parent.Transparency + 0.1
end

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

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

Simple:

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

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

for i=1,0, -0.1 do
    script.Parent.Transparency = (i)
    wait(0.01)
end
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 — 10y
Log in to vote
-1
Answered by
Bubby4j 231 Moderation Voter
10 years ago

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

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

If you want a function, here you go:

function animateTransparency(part, seconds, stepsize, endTransparency)
    while part.Transparency != endTransparency do
        if part.Transparency < endTransparency  then
            if part.Transparency + stepsize > endTransparency then
                part.Transparency =  endTransparency
            else
                part.Transparency = part.Transparency + stepsize
            end
        else
            if part.Transparency - stepsize < endTransparency then
                part.Transparency =  endTransparency
            else
                part.Transparency = part.Transparency - stepsize
            end
        end
        wait(seconds*stepsize)
    end
end

--And usage example:
animateTransparency(script.Parent, 0.1, 0.1, 0)

Answer this question