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:
01 | local tweenService = game:GetService( "TweenService" ) |
03 | local Bindable_Function = script:WaitForChild( "Fade_Function" ) |
05 | function 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) |
11 | function Fade(UI, Fade_Speed, Fade_To) |
14 | if UI:IsA( "Frame" ) then |
15 | local Tween_Properties = { Transparency = Fade_To } |
16 | Generate_And_Play_Tween(UI, Tween_Properties, Fade_Speed) |
20 | if UI:IsA( "ImageLabel" ) then |
21 | local Tween_Properties = { ImageTransparency = Fade_To } |
22 | Generate_And_Play_Tween(UI, Tween_Properties, Fade_Speed) |
26 | if UI:IsA( "TextLabel" ) or UI:IsA( "TextButton" ) then |
27 | local Tween_Properties = { TextTransparency = Fade_To } |
28 | Generate_And_Play_Tween(UI, Tween_Properties, Fade_Speed) |
32 | local function Fade_UI(UI, Fade_Speed, Fade_To, Fade_Children, Fade_Parent) |
36 | coroutine.wrap(Fade)(UI, Fade_Speed, Fade_To) |
40 | if Fade_Children = = true then |
41 | for _, child in UI:GetChildren() do |
42 | coroutine.wrap(Fade)(child, Fade_Speed, Fade_To) |
47 | Bindable_Function.OnInvoke = Fade_UI |