Im making a cube that will do aton of tweens as soon as another as finished. Here is a video of the full animation: https://streamable.com/g46bkg. My problem is that the code looks very hard to read and It seems like there is a more effective way instead of having 1000 tween.completed. Model: Model - Roblox
Code: |||
–Made by Its4Realzies
local sP = script.Parent
local center = sP.CentralMover
local scale = 1
–[[
The emblems move out of the box. (Squares will move farther than the pluses)
The two squares on the inside will pop out also and move so that they are spaced out equaly from the box to the end square. (They middle squares will rotate 45 degrees as they pop out)
One plus and one square will spin clockwise and the ones acrossed the cube from them will spin counter clockwise. (540 degrees all emblems EXEPT for the middle squares)
All the emblems will pop back into their orgininal position.
Box will break in half and rotates 180 degrees and unbreak (Just run the studio to see)
]]–
–Very handy tween function
local TweenService = game:GetService(“TweenService”)
local function doTween(tweenedObject, goal, duration, style, direction, repeatNum, repeats, tweenDelay)
–Length, Easing style, Direction, Number of times tween will repeat, Should tween repeat, Delay between tweens
local tweeningInformation = TweenInfo.new(duration, style, direction, repeatNum, repeats, tweenDelay)
local tween = TweenService:Create(tweenedObject, tweeningInformation, goal)
tween:Play()
return tween
end
–Welds two parts together
local function doWeld(p0, p1)
local weld = Instance.new(‘WeldConstraint’)
weld.Name = “WeldConstraint”
weld.Part0 = p0
weld.Part1 = p1
weld.Enabled = true
weld.Parent = p0
end
local tweenTarget = {}
local function doScramble()
–Move emblems out, rotate them, move them back in
local tweeningAxles = false
for i, emblem in ipairs(sP.Emblems:GetChildren()) do
local increment
local rotateTarget = CFrame.Angles(0, 0, 0)
local rotateDirection = CFrame.Angles(0, 0, math.rad(180))
if emblem.Name:match(“Hidden”) then
if emblem.Name == “HiddenSquare3” or emblem.Name == “HiddenSquare4” then
increment = 14
rotateDirection = CFrame.Angles(0, 0, math.rad(-202.5))
rotateTarget = CFrame.Angles(0, 0, math.rad(-45))
else
increment = 8
end
elseif emblem.Name:match(“Square”) then
increment = 20
rotateDirection = CFrame.Angles(0, 0, math.rad(180))
else
increment = 10
rotateDirection = CFrame.Angles(math.rad(-180), 0, 0)
end
increment = increment * scale
local offset = emblem.OuterPrime.Position - center.Position
local originalDistance = (center.Position - emblem.OuterPrime.Position).Magnitude
local targetPosition = center.Position + offset.Unit * (originalDistance + increment)
local targetCFrame = CFrame.new(targetPosition) * (emblem.OuterPrime.CFrame - emblem.OuterPrime.Position)
--Moves emblem out of box
script.GearsWhirl1:Play()
doTween(sP.PlusAxle, {Size = Vector3.new(scale * (22 + 20), 2 * scale, 2 * scale)}, 0.9, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out, 0, false, 0)
doTween(sP.SquareAxle, {Size = Vector3.new(scale * (22 + 40), 2 * scale, 2 * scale)}, 0.9, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out, 0, false, 0)
doTween(emblem.OuterPrime, {CFrame = targetCFrame * rotateTarget;}, 1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out, 0, false, 0).Completed:Connect(function(playbackState)
--Rotates emblem
script.GearsWhirl3:Play()
if emblem.Name == "HiddenSquare5" then
doWeld(emblem.OuterPrime, sP.Emblems.Square1.OuterPrime)
emblem.OuterPrime.Anchored = false
elseif emblem.Name == "HiddenSquare6" then
doWeld(emblem.OuterPrime, sP.Emblems.Square2.OuterPrime)
emblem.OuterPrime.Anchored = false
else
doTween(emblem.OuterPrime, {CFrame = emblem.OuterPrime.CFrame * rotateDirection;}, 1, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0.1).Completed:Connect(function(playbackState)
--Rotates emblem x 2
doTween(emblem.OuterPrime, {CFrame = emblem.OuterPrime.CFrame * rotateDirection;}, 1, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0).Completed:Connect(function(playbackState)
targetPosition = center.Position + offset.Unit * originalDistance
targetCFrame = CFrame.new(targetPosition) * (emblem.OuterPrime.CFrame - emblem.OuterPrime.Position)
script.GearsWhirl2:Play()
--Moves emblem into box
if tweeningAxles == false then
tweeningAxles = true
doTween(sP.PlusAxle, {Size = Vector3.new(22 * scale, 2 * scale, 2 * scale)}, 0.9, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out, 0, false, 0.1)
doTween(sP.SquareAxle, {Size = Vector3.new(22 * scale, 2 * scale, 2 * scale)}, 0.9, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out, 0, false, 0.1)
end
doTween(emblem.OuterPrime, {CFrame = targetCFrame;}, 1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out, 0, false, 0.1)
end)
end)
end
end)
end
wait(4)
--Setup for phase 2
sP.SquareAxle.Transparency = 1
sP.PlusAxle.Transparency = 1
for i, emblem in ipairs(sP.Emblems:GetChildren()) do
if emblem.Name:match("Hidden") then
emblem.OuterPrime.Transparency = 1
else
for i, part in ipairs(emblem:GetChildren()) do
if part.Name == "Inner" then
part.Anchored = true
part.WeldConstraint:Destroy()
elseif part.Name == "OuterPrime" then
doWeld(part, center)
part.Anchored = false
end
end
end
end
--Splits box and unspluts
--Moves center up
script.GearsWhirl4:Play()
doTween(center, {CFrame = center.CFrame + Vector3.new(0, 28 * scale, 0);}, 0.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.In, 0, false, 0).Completed:Connect(function(playbackState)
--Flips center 180 degrees
script.GearsWhirl1:Play()
doTween(center, {CFrame = center.CFrame * CFrame.Angles(0, math.rad(180), 0);}, 0.75, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out, 0, false, 0.1).Completed:Connect(function(playbackState)
--Moves center down
script.GearsWhirl4:Play()
doTween(center, {CFrame = center.CFrame + Vector3.new(0, -28 * scale, 0);}, 0.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out, 0, false, 0)
end)
end)
wait(4)
--Setup for phase 1
sP.SquareAxle.Transparency = 0
sP.PlusAxle.Transparency = 0
for i, emblem in ipairs(sP.Emblems:GetChildren()) do
if emblem.Name:match("Hidden") then
emblem.OuterPrime.Transparency = 0
else
for i, part in ipairs(emblem:GetChildren()) do
if part.Name == "Inner" then
doWeld(part, emblem.OuterPrime)
part.Anchored = false
elseif part.Name == "OuterPrime" then
part.Anchored = true
part.WeldConstraint:Destroy()
end
end
end
end
end
wait(1)
doScramble()
|||