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

How would I make my frame fade in and out? I can't make it fade out.

local gui = script.Parent.Parent.Parent.Frame

script.Parent.MouseButton1Click:Connect(function()
	
end)
repeat 
	wait(0.08)
	gui.BackgroundTransparency = gui.BackgroundTransparency - 0.1
until
gui.BackgroundTransparency == 0

I've been having trouble achieving this. I do however have achieved making the Frame transition into a visible state. I just can't seem to make it transition back into its invisible state.

2 answers

✓ Accepted answer

Instead of using repeat wait() until loop, use for i loop.

for i = 0, 1, .1 do -- first number is the start, second is the finish and third is the increment
	Frame.BackgroundTransparency = i
	wait()
end
--basically the loop starts at 0, then adds .1 until the progress(i) is 1

8 comments

0

That fades out the frame. If you want the frame to fade in, switch 1 and 0 and make .1 a negative (-.1)

0

note that when this code runs, other code below it won't run until the loop is completed

0

this fades it instantly as the wait() doesn't have an argument provided to it

0

adding a number inside the wait() will make the fading appear choppy. As long as the increment is low, there is no need for a number in wait()

0

But this fade is instantaeous

0

if you want super smooth fade this isn't the way to do it - refer to my answer

0

@thebayou it doesn't fade instantly. I fade my guis with a for loop and never put a number in wait

0

but you have no control over the duration of the animation

Due to floating point error, it's best not to compare a decimal number to another exact decimal number, as the binary representation of a number can cause what would in theory be two equivalent numbers to be different.

Because of this, when comparing the equality of two decimal numbers, either use an epsilon or use more general operators such as >= and <=.

local gui = script.Parent.Parent.Parent.Frame

script.Parent.MouseButton1Click:Connect(function()
	-- I'm assuming lines 06 to 10 were meant to go here?
	repeat
		wait(0.08)
		gui.BackgroundTransparency = gui.BackgroundTransparency - 0.1
	until gui.BackgroundTransparency <= 0.05	-- Accommodate for floating point error

	gui.BackgroundTransparency = 0		-- Reset the background transparency to make sure the gui is completely transparent
end)

EDIT

If you want the fade to be super smooth, here's something you could do:

local fadeDuration = 1		- - Change this to however lnog you want the fade animation to be (in seconds)

repeat
	local elapsed = wait()
	gui.BackgroundTransparency = gui.BackgroundTrasnparency - (elapsed*fadeDuration)/fadeDuration
until gui.BackgroundTransparency <= 0.01

2 comments

0

who cares the for loop is better

0

for loop is more efficient

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