We use cookies on this site to enhance your user experience

Tween Conflict

Tween Conflict

Oct 01 2018, 2:31 AM PST

This code sample includes a demonstration of tween conflict. A part is instanced in the Workspace, and two tweens are created that attempt to move the part in conflicting directions.

When both tweens are played, the first tween is cancelled and overwritten by the second tween. This can be seen as the part moves along the Y axis as opposed to the Z axis.

To further demonstrate this, connections have been made for both tweens to the Tween.Completed event. Upon playing the tweens, the following is printed.

tween1: Enum.PlaybackState.Cancelled
tween2: Enum.PlaybackState.Completed

These prints show that the first tween was cancelled (firing the Completed event) immediately upon the second tween being played. The second tween then went on to play until completion.

local TweenService = game:GetService("TweenService")

local part = Instance.new("Part")
part.Position = Vector3.new(0, 10, 0)
part.Anchored = true
part.Parent = game.Workspace
local tweenInfo = TweenInfo.new(5)

-- create two conflicting tweens (both trying to animate part.Position)
local tween1 = TweenService:Create(part, tweenInfo, {Position = Vector3.new(0, 10, 20)})
local tween2 = TweenService:Create(part, tweenInfo, {Position = Vector3.new(0, 30, 0)})

-- listen for their completion status
	print("tween1: "..tostring(playbackState))
	print("tween2: "..tostring(playbackState))

-- try to play them both