I wanted to know how to create a tween for a gui and all of its children so they can all tween at the same time.
I have tried this:
local button = script.Parent
local bpos = button.Position
local tweens = game:GetService("TweenService")
local children = button.Parent:GetChildren()
button.MouseButton1Click:Connect(function()
local Info = TweenInfo.new(1)
local Tween = tweens:Create(button.Parent,children,Info,{BackgroundTransparency=1})
Tween:Play()
end)
But it gave me an error saying:
“Unable to cast value to Object”
Use a for loop to go through all of the GUI’s children, create the tween in the for loop, and it should work.
You can do it like this:
local button = script.Parent
local bpos = button.Position
local tweens = game:GetService("TweenService")
button.MouseButton1Click:Connect(function()
for _, obj in pairs(button.Parent:GetChildren()) do
if obj:IsA("Frame") or obj:IsA("TextButton") or obj:IsA("TextLabel") or obj:IsA("TextBox") or obj:IsA("ImageButton") or obj:IsA("ImageLabel") then
tweens:Create(obj, TweenInfo.new(1), {BackgroundTransparency=1}):Play()
end
end
end)
A simple for loop should do the job. You must check if the item is an actual GuiObject using the IsA method. Here’s how it should be:
local tweenService = game:GetService("TweenService") -- Get TweenService
local tweenInfo = TweenInfo.new(
.5, -- Time
Enum.EasingStyle.Sine, -- EasingStyle
Enum.EasingDirection.Out, -- EasingDirection
0, -- Repeat count
false, -- Reverse
0 -- Delay time between repeats
)
local button = script.Parent -- The button
button.MouseButton1Click:Connect(function() -- Detecting the click
for i, v in pairs(button.Parent:GetChildren()) do --[=[
Looping through children.
You can also use button.Parent:GetDescendants() if you want to fade out everything inside button.Parent and not just the children
]=]
if not v:IsA("GuiObject") then continue end -- If `v` isn't a UI object then skip it
local tween = tweenService:Create(
i, -- The instance
tweenInfo, -- TweenInfo
{ -- Table holding all properties we need to change
BackgroundTransparency = 1 -- Means the background will be invisible.
}
)
tween:Play() -- Playing the tween
-- tween.Completed:Wait() -- Waiting for the tween to finish, better to have it commented.
end
end)