Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

For Loops & Transparency

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

✓ Accepted answer

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

3 comments

0

Note that because this will loop 10 times, it will result in a total time of 1 second.

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.

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.

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

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

1 comment

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.

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.