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

Can someone please take the time to elaborately explain how this fade transparency script works?

I know Its supposed to make a frame fade in and fade out in its transparency smoothly, But I want to know how this script works so I can use this in other scripts. So can maybe one day smoothly fade and out color.

fade = script.Parent
fadeGoal = 0
fadeRate = 0.05

function updateFade()
	local current = fade.BackgroundTransparency
	if current < fadeGoal then
		fade.BackgroundTransparency = math.min(fadeGoal,current+fadeRate)
	elseif current > fadeGoal then
		fade.BackgroundTransparency = math.max(fadeGoal,current-fadeRate)
	else
		fade.BackgroundTransparency = fadeGoal
	end
end

player = game.Players.LocalPlayer
character = player.Character or player.CharacterAdded:wait()
humanoid = player.Character:WaitForChild("Humanoid")
rs = game:GetService("RunService")

fadeGoal = 1

humanoid.Died:connect(function ()
	wait(1)
	fadeGoal = 0
	player.CharacterAdded:wait()
	fadeGoal = 1
end)

rs.RenderStepped:connect(updateFade)

fade.Visible = true

1 comment

0

:c

1 answer

✓ Accepted answer

Basically, transparency 1 is invisible and 0 is completely there. So its taking its current transparency and subtracting it by 0.05 and it continues doing this until the transparency equals 0, thus making it visible.(This is for fading in. For fading out it would take the current transparency and add 0.05. I will add more to this in a bit but for now I am just putting this out for you to read.

Edit: This is completely how this script works.

fade = script.Parent --The gui
fadeGoal = 0 -- What the transparency should end at. This is 0 so it is fading in.
fadeRate = 0.05 -- How fast it should fade

function updateFade()
    local current = fade.BackgroundTransparency --The background
    if current < fadeGoal then --If the current transparency is smaller that the the goal
        fade.BackgroundTransparency = math.min(fadeGoal,current+fadeRate) -- If true subtract the current transparency from the fade rate 
    elseif current > fadeGoal then --
        fade.BackgroundTransparency = math.max(fadeGoal,current-fadeRate) 
    else
        fade.BackgroundTransparency = fadeGoal -- if the transparency is equel to or bigger than goal sets transparency to goal
    end
end

player = game.Players.LocalPlayer
character = player.Character or player.CharacterAdded:wait()
humanoid = player.Character:WaitForChild("Humanoid")
rs = game:GetService("RunService")

fadeGoal = 1

humanoid.Died:connect(function ()
    wait(1)
    fadeGoal = 0
    player.CharacterAdded:wait()
    fadeGoal = 1
end)

rs.RenderStepped:connect(updateFade)

fade.Visible = true

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