Stop TweenService from overriding each other

Say, I have the following code,

TweenService:Create(obj, TweenInfo.new(10), {Position += Vector3.new(0,10,0)}):Play()

task.wait(2.5)

TweenService:Create(obj, TweenInfo.new(2), {Position += Vector3.new(0,-5,0)}):Play()

The second tween will override the first tween.

Is there any way for both to continue running?

For instance, from the example above, the first tween is moving the object upwards at the speed of 1 stud per second for 10 seconds. The second tween is moving the object downwards at the speed of 2.5 studs per second for 2 seconds. So, after 2.5 second passed, when the second tween runs, the object should move down 1.5 (2.5 - 1) studs per second for 2 seconds, then the first tween should continue playing for 5.5 seconds.

1 Like

There’s not a way to change that behavior, unfortunately - you could, however, manually interpolate the position of an object using a ‘for’ loop using the :lerp method of a Vector3 value

2 Likes

going off of what @Geomaster said, you could just use a simple lerp function:

local function Lerp(a, b, t)
	return a + (b - a) * t
end

And then use this to tween it:

local OverallTime = 0

local function Tween(Part, DeltaTime, NewPosition, OldPosition, TimeForTween, Name)
    OverallTime += DeltaTime
    if OverallTime >= TimeForTween then
        Part.Position = NewPosition
        RunService:UnBindRenderToStep(Name)
    end
    Part.Position = Lerp(OldPosition, NewPosition, OverallTime/ TimeForTween)
end

local function Tween1(DeltaTime)
    Tween(
        workspace.Part, ---Part or value
        DeltaTime. ---The delta Time
        Vector3.new(0, 10, 0) --- Position To Tween To
        Vector3.new(Part.Position.X, 0, Part.Position.Z) -- the old position of the part
        10 --The time for the tween
        "Tween1" --Name of the tween
    )
end

local function Tween2()
    Tween(
        workspace.Part, ---Part or value
        DeltaTime. ---The delta Time
        Vector3.new(0, -5, 0) --- Position To Tween To
        Vector3.new(Part.Position.X, 0, Part.Position.Z) -- the old position of the part
        2 --The time for the tween
        "Tween2" --Name of the tween
    )
end

RunService:BindRenderToStep("Tween1", 0, Tween1)

task.wait(2.5)

RunService:BindRenderToStep("Tween2", 0, Tween2)
2 Likes

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