How do I create a tween for a gui and all of its children?

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”

  1. Loop through all the children
  2. Inside of that loop create a tween
  3. Play the tween

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)
1 Like

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)
1 Like

Thanks for your help! This works.

(P.S, Thanks for all your guys’ help.)

1 Like

No problem! :grin:

character limit

And… it doesn’t work anymore. I get the same error as last time :confused:

That sucks! Does it give you a specific line where it breaks? Can you show me the output?

Oh, I realized I left my own code in there, sorry for the reply lol.

(As a side note, your code doesn’t get rid of the text in my guis, any ideas?)

It’s okay lol! It happens to the best of us!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.