How do i loop a tween like this one?

Hey there, quick question!

Im goofing arround with tweenings and i decided to try out this plugin (wich makes easy and simple tweenings)

The plugin created the tweening scripts and i tried to loop both tweens (because the plugin can’t do it)

So, i just mixed both scripts and added a Return at the end:

local tweenservice = game:GetService("TweenService")

local target = game:GetService("Workspace"):FindFirstChild(script.Target.Value)

local info = TweenInfo.new(

script.Duration.Value,

Enum.EasingStyle.Cubic,

Enum.EasingDirection.InOut,

0,

false,

0

)

local goal = {}

goal.CFrame = target.CFrame

local tween = tweenservice:Create(script.Parent, info, goal)

tween:Play()

--------------------------------

wait(25)

--------------------------------

local tweenservice = game:GetService("TweenService")

local target = game:GetService("Workspace"):FindFirstChild(script.Target2.Value)

local info = TweenInfo.new(

script.Duration2.Value,

Enum.EasingStyle.Linear,

Enum.EasingDirection.Out,

0,

false,

0

)

local goal = {}

goal.CFrame = target.CFrame

local tween = tweenservice:Create(script.Parent, info, goal)

tween:Play()

---------------------------------------------------

wait(25)

-----------------------------------------------------

return

This code is not looping the tween, it only works one time!

EDIT: This script has some values parented to it (if you haven’t noticed) This parents are:

  • Duration
  • Duration2
  • Target
  • Target2

Thank you!

4 Likes

Edit: As pullman45 said, using -1 instead of math.huge is a much better option.

If you don’t know scripting, you can change the repeat count parameter to math.huge. This isn’t perfect, as math.huge is just the largest possible number, and isn’t infinite. You probably will never run into the problem of having your tween stop, because the number math.huge represents is huge.

Examples

If you don’t know any programming, something like this will work:

-- ...

local info = TweenInfo.new(

script.Duration2.Value,

Enum.EasingStyle.Linear,

Enum.EasingDirection.Out,

-1, -- repeats tween forever

false, --It the tween reverses after completing

0

)

-- ...

If you do know any programming, you can do something like this:

-- ...

local tween = tweenservice:Create(script.Parent, info, goal)

tween:Play()
spawn(function()
    while true do
        tween.Completed:Wait()
        -- You may need to add code to reset properties.
        tween:Play()
    end
end)

-- ...

This isn’t the best way, though the other ways are a bit more confusing.


Hope this helps :smile:

If you want to do one tween then another tween in a loop:

-- ...  (create tweens, etc.)

local tween1 --set to your first tween
local tween2 --set to your second tween

while true do --Note: This basically stops your code, so nothing below it runs. Add the spawn() function to it like the example above if you don't want that.
    tween1:Play()
    tween1.Completed:Wait()
    tween2:Play()
    tween2.Completed:Wait()
end

4 Likes

Oh, great, thanks!

Question: Does the code works for over than 4 tweens?

1 Like

Yup

2 Likes

Just set it to -1.

As stated by the wiki:
“The correct way to make a tween play indefinitely is to set RepeatCount to -1. Developers should avoid using large numbers (or math.huge) as a substitute as this is unstable and may stop working at any point.”

1 Like

@PseudoPerson I putted all the tweenings that i need, but it doesn’t work

Here is what i did:

-- ... (create tweens, etc.)

local tween1

local tweenservice = game:GetService("TweenService")

local target = game:GetService("Workspace"):FindFirstChild(script.Target.Value)

local info = TweenInfo.new(

script.Duration.Value,

Enum.EasingStyle.Cubic,

Enum.EasingDirection.InOut,

0,

false,

0

)

local goal = {}

goal.CFrame = target.CFrame

local tween = tweenservice:Create(script.Parent, info, goal)

---------------

local tween2

local tweenservice = game:GetService("TweenService")

local target = game:GetService("Workspace"):FindFirstChild(script.Target2.Value)

local info = TweenInfo.new(

script.Duration2.Value,

Enum.EasingStyle.Cubic,

Enum.EasingDirection.InOut,

0,

false,

0

)

local goal = {}

goal.CFrame = target.CFrame

local tween = tweenservice:Create(script.Parent, info, goal)

-------------------

local tween3

local tweenservice = game:GetService("TweenService")

local target = game:GetService("Workspace"):FindFirstChild(script.Target3.Value)

local info = TweenInfo.new(

script.Duration3.Value,

Enum.EasingStyle.Cubic,

Enum.EasingDirection.InOut,

0,

false,

0

)

local goal = {}

goal.CFrame = target.CFrame

local tween = tweenservice:Create(script.Parent, info, goal)

-----------------------

local tween4

local tweenservice = game:GetService("TweenService")

local target = game:GetService("Workspace"):FindFirstChild(script.Target4.Value)

local info = TweenInfo.new(

script.Duration4.Value,

Enum.EasingStyle.Cubic,

Enum.EasingDirection.InOut,

0,

false,

0

)

local goal = {}

goal.CFrame = target.CFrame

local tween = tweenservice:Create(script.Parent, info, goal)

while true do --Note: This basically stops your code, so nothing below it runs. Add the spawn() function to it like the example above if you don't want that.

tween1:Play()

tween1.Completed:Wait()

wait(20)

tween2:Play()

tween2.Completed:Wait()

wait(20)

tween3:Play()

tween3.Completed:Wait()

wait(20)

tween4:Play()

tween4.Completed:Wait()

wait(20)

end

This is what output drops: 13:12:51.515 Workspace.TransportPart.TweenScript:86: attempt to index nil with ‘Play’ - Server - TweenScript:86

Sorry if im terrible at scripting :sweat_smile:


Thank you!

So every time you make a variable it’s given a name for the code to reference it by. So you should name each one differently. For example, when you make your first tween call it tween1 instead of just tween. You should do the same for the other variables, like target and goal, but you don’t NEED to because they get over written.

Here is some revised code.

-- ... (create tweens, etc.)



local tweenservice = game:GetService("TweenService")

local target1 = game:GetService("Workspace"):FindFirstChild(script.Target.Value)

local info1 = TweenInfo.new(

script.Duration.Value,

Enum.EasingStyle.Cubic,

Enum.EasingDirection.InOut,

0,

false,

0

)

local goal1 = {}

goal.CFrame = target1.CFrame

local tween1 = tweenservice:Create(script.Parent, info1, goal1)

---------------

-- local tweenservice = game:GetService("TweenService")

local target2 = game:GetService("Workspace"):FindFirstChild(script.Target2.Value)

local info2 = TweenInfo.new(

script.Duration2.Value,

Enum.EasingStyle.Cubic,

Enum.EasingDirection.InOut,

0,

false,

0

)

local goal2 = {}

goal.CFrame = target2.CFrame

local tween2 = tweenservice:Create(script.Parent, info2, goal2)

-------------------

-- local tweenservice = game:GetService("TweenService")

local target3 = game:GetService("Workspace"):FindFirstChild(script.Target3.Value)

local info3 = TweenInfo.new(

script.Duration3.Value,

Enum.EasingStyle.Cubic,

Enum.EasingDirection.InOut,

0,

false,

0

)

local goal3 = {}

goal.CFrame = target3.CFrame

local tween3 = tweenservice:Create(script.Parent, info3, goal3)

-----------------------

local tween4

-- local tweenservice = game:GetService("TweenService") you don’t need this line because you already made a variable with the same value

local target4 = game:GetService("Workspace"):FindFirstChild(script.Target4.Value)

local info4 = TweenInfo.new(

script.Duration4.Value,

Enum.EasingStyle.Cubic,

Enum.EasingDirection.InOut,

0,

false,

0

)

local goal4 = {}

goal4.CFrame = target.CFrame

local tween4 = tweenservice:Create(script.Parent, info4, goal4)

while true do --Note: This basically stops your code, so nothing below it runs. Add the spawn() function to it like the example above if you don't want that.

tween1:Play()

tween1.Completed:Wait()

wait(20) -- this line makes the a 20 second delay between each tween. Keep if you want that.

tween2:Play()

tween2.Completed:Wait()

wait(20)

tween3:Play()

tween3.Completed:Wait()

wait(20)

tween4:Play()

tween4.Completed:Wait()

wait(20)

end

I edited that on mobile, so I might have made a mistake. If there is one, look through the variable names for something that doesn’t fit the pattern. Also, make sure you have some Vector3Values in the script’s parent that are named Target1, Target2, and Target3.
Edit: wait maybe they are StringValues. Just make sure something like that exists.

1 Like