Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How can I properly optimize this GUI fade script?

Asked by 2 years ago

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.

01local Bindable_Function = script:WaitForChild("Fade_Function")
02 
03function Fade(UI, Fade_Speed, Fade_To)
04 
05    -- Check if it is a Frame
06    if UI:IsA("Frame") then
07        if Fade_To < UI.Transparency then
08            while UI.Transparency >= Fade_To do
09                UI.Transparency -= Fade_Speed
10                wait(0.01)
11            end
12        else
13            while UI.Transparency <= Fade_To do
14                UI.Transparency += Fade_Speed
15                wait(0.01)
View all 66 lines...
0
Does it work? T3_MasterGamer 2189 — 2y

1 answer

Log in to vote
1
Answered by 2 years ago

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:

01local tweenService = game:GetService("TweenService")
02 
03local Bindable_Function = script:WaitForChild("Fade_Function")
04 
05function Generate_And_Play_Tween(UI, Tween_Properties, Fade_Speed)
06    local tweenInfo = TweenInfo.new(Fade_Speed)
07    local tween = tweenService:Create(UI, tweenInfo, Tween_Properties)
08    tween:Play()
09end
10 
11function Fade(UI, Fade_Speed, Fade_To)
12 
13    -- Check if it is a Frame
14    if UI:IsA("Frame") then
15        local Tween_Properties = {Transparency = Fade_To}
View all 47 lines...
0
Thanks! I'll be sure to try this out! Boomboyag 14 — 2y
Ad

Answer this question