How can I get the children of my table to tween?

Hello,

I’m working on tweening frames, I’m using TweenService to have the frames go up and down on a players screen. I have a table with all of the tweens and I want to play them all at once. Although, the only way I can achieve this is by typing out tweens["TweenName"]:Play() for each tween. Is there a more efficient way of achieving this?

EDIT: I forgot to change the positions that the frame will tween to in the table. Therefore all of them are UDim2.new(0.05,0,0.35,0)

local DataCrack = script.Parent
local Values = DataCrack:FindFirstChild("Values")
local Lines = DataCrack:FindFirstChild("Lines")
local MID = DataCrack:FindFirstChild("MID")
local TweenService = game:GetService("TweenService")

local tweeninfo1 = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.In, -1, true, 0)
local tweeninfo2 = TweenInfo.new(0.45, Enum.EasingStyle.Linear, Enum.EasingDirection.In, -1, true, 0)
local tweeninfo3 = TweenInfo.new(0.4, Enum.EasingStyle.Linear, Enum.EasingDirection.In, -1, true, 0)
local tweeninfo4 = TweenInfo.new(0.35, Enum.EasingStyle.Linear, Enum.EasingDirection.In, -1, true, 0)
local tweeninfo5 = TweenInfo.new(0.3, Enum.EasingStyle.Linear, Enum.EasingDirection.In, -1, true, 0)
local tweeninfo6 = TweenInfo.new(0.25, Enum.EasingStyle.Linear, Enum.EasingDirection.In, -1, true, 0)
local tweeninfo7 = TweenInfo.new(0.2, Enum.EasingStyle.Linear, Enum.EasingDirection.In, -1, true, 0)
local tweeninfo8 = TweenInfo.new(0.15, Enum.EasingStyle.Linear, Enum.EasingDirection.In, -1, true, 0)

Values.Starting.Changed:Connect(function()
	if Values.Starting.Value == true then
		local tweens = {
			tween1 = TweenService:Create(Lines.First, tweeninfo1, {Position = UDim2.new(0.05,0,0.35,0)}),
			tween2 = TweenService:Create(Lines.Second, tweeninfo2, {Position = UDim2.new(0.05,0,0.35,0)}),
			tween3 = TweenService:Create(Lines.Third, tweeninfo3, {Position = UDim2.new(0.05,0,0.35,0)}),
			tween4 = TweenService:Create(Lines.Fourth, tweeninfo4, {Position = UDim2.new(0.05,0,0.35,0)}),
			tween5 = TweenService:Create(Lines.Fifth, tweeninfo5, {Position = UDim2.new(0.05,0,0.35,0)}),
			tween6 = TweenService:Create(Lines.Sixth, tweeninfo6, {Position = UDim2.new(0.05,0,0.35,0)}),
			tween7 = TweenService:Create(Lines.Seventh, tweeninfo7, {Position = UDim2.new(0.05,0,0.35,0)}),
			tween8 = TweenService:Create(Lines.Eighth, tweeninfo8, {Position = UDim2.new(0.05,0,0.35,0)})
		}
		
		tweens.tween1:Play()
		tweens.tween2:Play()
		tweens.tween3:Play()
		tweens.tween4:Play()
		tweens.tween5:Play()
		tweens.tween6:Play()
		tweens.tween7:Play()
		tweens.tween8:Play()
	end
end)
1 Like

You can use a “for” loop to loop through all of the objects in a table. You could use it in your case to :Play() all of the tweens in your table.

Example:

for number, object in pairs(tweens) do
    object:Play()
end

https://developer.roblox.com/en-us/articles/Loops

Hope this helps!

3 Likes

Wow, I can’t believe I didn’t think of that. Thank you for your help!

1 Like