UI Fade Module
This wonderful module was created by @7kayoh, but she has granted me permission to share it with you all!
Source
local module = {}
local TweenService = game:GetService("TweenService")
local function GetProperty(Object)
if Object:IsA("TextLabel") or Object:IsA("TextButton") or Object:IsA("TextBox") then
return "TextTransparency"
elseif Object:IsA("ViewportFrame") or Object:IsA("ImageButton") or Object:IsA("ImageLabel") then
return "ImageTransparency"
elseif Object:IsA("Frame") then
return "BackgroundTransparency"
elseif Object:IsA("ScrollingFrame") then
return "ScrollBarImageTransparency"
end
end
function module.FadeIn(Object, FadeTime)
local TI = TweenInfo.new(FadeTime, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
local Table = Object:GetDescendants()
Table[#Table + 1] = Object
for i,v in pairs(Table) do
local Property = GetProperty(v)
if Property then
if v:FindFirstChild("DefaultTransparencyValue") then
TweenService:Create(v, TI, {[Property] = v:FindFirstChild("DefaultTransparencyValue").Value}):Play()
else
local DefaultTransparencyValue = Instance.new("NumberValue")
DefaultTransparencyValue.Name = "DefaultTransparencyValue"
DefaultTransparencyValue.Value = v[Property]
DefaultTransparencyValue.Parent = v
v[Property] = 1
TweenService:Create(v, TI, {[Property] = v:FindFirstChild("DefaultTransparencyValue").Value}):Play()
end
end
Property = nil
end
TI = nil
Table = nil
end
function module.FadeOut(Object, FadeTime)
local TI = TweenInfo.new(FadeTime, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
local Table = Object:GetDescendants()
Table[#Table + 1] = Object
for i,v in pairs(Table) do
local Property = GetProperty(v)
if Property then
if v:FindFirstChild("DefaultTransparencyValue") then
TweenService:Create(v, TI, {[Property] = 1}):Play()
else
local DefaultTransparencyValue = Instance.new("NumberValue")
DefaultTransparencyValue.Name = "DefaultTransparencyValue"
DefaultTransparencyValue.Value = v[Property]
DefaultTransparencyValue.Parent = v
TweenService:Create(v, TI, {[Property] = 1}):Play()
end
end
Property = nil
end
TI = nil
Table = nil
end
return module
How does it work? How do I use it?
It’s quite simple. Once you require the module, all you have to do is call the FadeIn
and FadeOut
functions:
local FadeModule = require(game:GetService("ReplicatedStorage").FadeModule)
local Object = Path.To.UI
FadeModule.FadeIn(Object, 2)
FadeModule.FadeOut(Object, 2)
The two functions are extremely straight forward, and both work the same way:
Examples of usage are shown above.
FadeIn(instance : Object, int : Duration)
FadeOut(instance : Object, int : Duration)
Calling a fade function on a UI instance will automatically fade the children of the UI, all in one simple line without the need to call it on any other element.
Calling FadeIn
will remember the instance’s original transparency value.
The module is not fading a certain element of my UI!
Not every single object that is able to be used in UI is pre-defined. If you notice that one is missing and won’t fade, add to the bottom of the GetProperty function in the source code with the following:
elseif Object:IsA("ClassName") then
return "Property"
Example
elseif Object:IsA("ScrollingFrame") then
return "ScrollBarImageTransparency"
Let me know if you experience any issues with this module, however let’s not steal the credit from our almighty nanako who created this