How to Tween children inside a folder?

I’m rather new to Luau and trying to become better with it.
I’m currently having trouble understanding how managing children works within Luau.

What I’m Trying to Achieve

  • I want to spin multiple cubes within my spawn area.
  • I have 3 cubes inside a folder.
  • I want to have just one script do all the work for me rather than putting a script in each cube.

The Code I Have Now

local cube = script.Parent
local tween = game:GetService('TweenService')


local tweenInfo = TweenInfo.new(
	5, -- time to complete the tween
	Enum.EasingStyle.Linear, -- easing style of the tween
	Enum.EasingDirection.InOut, -- direction of the easing style (in this case it's both)
	-1, -- number of times to repeat (-1 means infinite)
	false, -- reverses the animation when it repeats (true means yes)
	0) -- delay before starting the tween

local Goal = {
	CFrame = cube.CFrame * CFrame.Angles(0, math.rad(90), 0)
} 

local spiningTween = tween:Create(cube, tweenInfo, Goal)

spiningTween:Play()

This code currently works fine with having the script in the cube by itself.

How would I go about having the script access the children of the folder to all spin with this TweenInfo?

1 Like

I think you could do something like,

local cubes = cube.Parent:GetChildren("Part")
-- replace the cube.Parent with the folder, and the Part with the name of the parts.

for i, Cube in pairs(cubes) do

That should make each part inside the folder perform that function individually.
GetChildren info: Instance | Documentation - Roblox Creator Hub

Use a for loop and folder:GetChildren(). Example:

local tweenService = game:GetService("TweenService")

local cubes = cubeFolder:GetChildren() -- Replace cubeFolder with path to folder

local tweenInfo = TweenInfo.new(
	5, -- time to complete the tween
	Enum.EasingStyle.Linear, -- easing style of the tween
	Enum.EasingDirection.InOut, -- direction of the easing style (in this case it's both)
	-1, -- number of times to repeat (-1 means infinite)
	false, -- reverses the animation when it repeats (true means yes)
	0) -- delay before starting the tween

for _, cube in ipairs(cubes) do
    tweenService:Create(cube, tweenInfo, {
	    CFrame = cube.CFrame * CFrame.Angles(0, math.rad(90), 0)
    }):Play()
end
1 Like

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