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

How can I properly optimize this GUI fade script?

Hi y'all! I've been working on this piece of code that will slowly fade a given UI's transparency. I'm just wondering if there is anything I can do to improve it. Any help or tips would be greatly appreciated.

local Bindable_Function = script:WaitForChild("Fade_Function")

function Fade(UI, Fade_Speed, Fade_To)
	
	-- Check if it is a Frame
	if UI:IsA("Frame") then
		if Fade_To < UI.Transparency then
			while UI.Transparency >= Fade_To do
				UI.Transparency -= Fade_Speed
				wait(0.01)
			end
		else
			while UI.Transparency <= Fade_To do
				UI.Transparency += Fade_Speed
				wait(0.01)
			end
		end
	end
	
	-- Check if it is an Image
	if UI:IsA("ImageLabel") then
		if Fade_To < UI.ImageTransparency then
			while UI.ImageTransparency >= Fade_To do
				UI.ImageTransparency -= Fade_Speed
				wait(0.01)
			end
		else
			while UI.ImageTransparency <= Fade_To do
				UI.ImageTransparency += Fade_Speed
				wait(0.01)
			end
		end
	end
	
	-- Check if it is a text label or button
	if UI:IsA("TextLabel") or UI:IsA("TextButton") then
		if Fade_To < UI.TextTransparency then
			while UI.TextTransparency >= Fade_To do
				UI.TextTransparency -= Fade_Speed
				wait(0.01)
			end
		else
			while UI.TextTransparency <= Fade_To do
				UI.TextTransparency += Fade_Speed
				wait(0.01)
			end
		end
	end
end

local function Fade_UI(UI, Fade_Speed, Fade_To, Fade_Children, Fade_Parent)
	
	-- Check if we want to fade the given UI
	if Fade_Parent then
		coroutine.wrap(Fade)(UI, Fade_Speed, Fade_To)
	end
	
	-- Check if we want to fade the children
	if Fade_Children == true then
		for _, child in UI:GetChildren() do
			coroutine.wrap(Fade)(child, Fade_Speed, Fade_To)
		end
	end
end

Bindable_Function.OnInvoke = Fade_UI

1 comment

0

Does it work?

1 answer

✓ Accepted answer

Use Tweens, and combine your functions. In my code, Fade_Speed describes the number of seconds to fully fade. New code with tweenservice instead of loops, and some functions simplified:

local tweenService = game:GetService("TweenService")

local Bindable_Function = script:WaitForChild("Fade_Function")

function Generate_And_Play_Tween(UI, Tween_Properties, Fade_Speed)
    local tweenInfo = TweenInfo.new(Fade_Speed)
    local tween = tweenService:Create(UI, tweenInfo, Tween_Properties)
    tween:Play()
end

function Fade(UI, Fade_Speed, Fade_To)

    -- Check if it is a Frame
    if UI:IsA("Frame") then
        local Tween_Properties = {Transparency = Fade_To}
        Generate_And_Play_Tween(UI, Tween_Properties, Fade_Speed)
    end

    -- Check if it is an Image
    if UI:IsA("ImageLabel") then
        local Tween_Properties = {ImageTransparency = Fade_To}
        Generate_And_Play_Tween(UI, Tween_Properties, Fade_Speed)
    end

    -- Check if it is a text label or button
    if UI:IsA("TextLabel") or UI:IsA("TextButton") then
        local Tween_Properties = {TextTransparency = Fade_To}
        Generate_And_Play_Tween(UI, Tween_Properties, Fade_Speed)
    end
end

local function Fade_UI(UI, Fade_Speed, Fade_To, Fade_Children, Fade_Parent)

    -- Check if we want to fade the given UI
    if Fade_Parent then
        coroutine.wrap(Fade)(UI, Fade_Speed, Fade_To)
    end

    -- Check if we want to fade the children
    if Fade_Children == true then
        for _, child in UI:GetChildren() do
            coroutine.wrap(Fade)(child, Fade_Speed, Fade_To)
        end
    end
end

Bindable_Function.OnInvoke = Fade_UI

1 comment

0

Thanks! I'll be sure to try this out!

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