Hey everyone! I’ve been recently wondering what the most efficient way is to make a frame fade out. Now, we obviously know that we can utilize the TweenService for this, but what about all of the UI elements inside the frame? They won’t be fading out, only the background frame will.
You could just use a for loop and get the frame’s children/descendants, though yet again, you’re gonna have to go through the process of if statements to check what type of object it is (ImageLabel, TextLabel, Frame, TextButton, etc).
What I do to achieve this is creating a transparent frame for all my Buttons, Labels, etc. And have another frame that is separate for the background that isn’t transparent. There is probably other more efficient ways to go about this but this is what works for me.
and then it just continues until 1. I know it’s not THAT efficient, and there’s probably a much better way to do it with tweening*, but for someone who isn’t that good at scripting, it works just fine for me. The same can be done with text labels, image labels, etc. Let me know if you’d like me to reply with an example of what the script does*. ^^
See? I told you there would be a much better way. Like I mentioned, I’m not a full blown scripter, but it works perfectly fine for me, and is actually quite smooth. At least it fades out smoothly anyways. I’ll test this out though! This could also help OP.
Well uh, yeah this could help people like me, and others as well! And with the uhm… interesting script I posted you can do the same with any amount of UI you like. Only, it might reach 100 lines of script VERY fast.
Yeah so with the script I posted I did this with it, but I decided to leave the frame in the background alone because I got lazy. However I did this effect with the text. It’s smooth IMO, but might not to better developers than me. So in case you’re curious, here’s the video.
It also fades back out, but Gyazo only records 6 seconds for non premium users.
Not sure if this has been answered but TweenService for every single descendant. It can be easily done with an iterating loop and a basic function that handles tween info.
You asked for efficiency, none of the answers here have been optimized except for @Vyntrick’s, but that one isn’t too beautiful.
local frame = -- Reference Frame here.
local frameWait = game:GetService("RunService").RenderStepped;
local descendants = frame:GetDescendants(); -- Optimization, no need to re-find descendants.
for i = 0, 1, 0.05 do -- Adjust last number for step. This will go 20 times until it reaches transparency of 1.
frame.Transparency = i;
for _, currObj in pairs(descendants) do
if currObj:IsA("GuiObject") then -- GuiObject inheritance of "Transparency" hidden property.
currObj.Transparency = i;
end;
end;
frameWait:Wait();
end;
I tried this with the TweenService instead of an iteration and it works, though the only problem being that it sets the BackgroundTransparency of TextLabels to 0, which I do not want. (I was doing fade in and out.)
May I ask why you care so much about optimization? It really doesn’t matter unless you’re running the code every frame or something, which would be a little crazy for something like tweening. I would recommend just looping through each element checking if they belong to a super class with transparency or image transparency. Feel free to ask me for some api about that.
I ended up with this function that works for me quite well.
local function tweenThingy(frame, transparency)
tweenService:Create(frame, TweenInfo.new(0.5), {Transparency = transparency}):Play()
for _, v in pairs(frame:GetDescendants()) do
if v:IsA("GuiObject") then
if v:IsA("TextLabel") then
tweenService:Create(v, TweenInfo.new(0.5), {TextTransparency = transparency}):Play()
else
tweenService:Create(v, TweenInfo.new(0.5), {Transparency = transparency}):Play()
end
end
end
end