PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

Play

The Play function starts the playback of its Tween. Note that if a tween has already begun calling Play will have no effect unless the tween has finished or has been stopped (either by TweenBase/Cancel or TweenBase/Pause).

Multiple tweens can be played on the same object at the same time, but they must not be animating the same property. If two tweens attempt to modify the same property, the initial tween will be cancelled and overwritten by the most recent tween (see examples).

Returns

Return Type Summary

Code Samples


Tween Conflict

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
tween1.Completed:Connect(function(playbackState)
	print("tween1: "..tostring(playbackState))
end)
tween2.Completed:Connect(function(playbackState)
	print("tween2: "..tostring(playbackState))
end)

-- try to play them both
tween1:Play()
tween2:Play()

Tween Creation

In this example a Tween is created to animate the position and color of a Part. Because the position and color are part of the same tween, they will change at the exact same rate and will reach their goal at the same time.

local TweenService = game:GetService("TweenService")
 
local part = Instance.new("Part")
part.Position = Vector3.new(0, 10, 0)
part.Color = Color3.new(1, 0, 0)
part.Anchored = true
part.Parent = game.Workspace
 
local goal = {}
goal.Position = Vector3.new(10, 10, 0)
goal.Color = Color3.new(0, 1, 0)
 
local tweenInfo = TweenInfo.new(5)
 
local tween = TweenService:Create(part, tweenInfo, goal)
 
tween:Play()