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

Completed

The Completed event of a Tween fires when the tween finishes playing. This will happen either when the tween naturally finishes playing, or if it is stopped with TweenBase/Cancel.

A function connected to this event will be passed the PlaybackState of the tween when it finished to give indication about why the tween ended. If the tween finishes naturally the status will be Completed, if cancelled then the state will be Cancelled. Note that calling TweenBase/Pause will not fire the Completed event.

Parameters

Name Type Default Description

The Enum/PlaybackState of the tween upon completion.


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()

Verifying a Tween has Completed

This code sample includes an example of how Tween.Completed can be used to determine if a Tween has been successfully completed, or cancelled.

In this case a part is instanced and tweened towards 0, 0, 0. Once the tween has completed, if the final PlaybackState is Completed then the part will explode. Were the tween to be cancelled prior to completion, the explosion would not be created.

This method can be used to link tweens to other effects, or even chain several tweens to play after each other.

local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")

local part = Instance.new("Part")
part.Position = Vector3.new(0, 50, 0)
part.Anchored = true
part.Parent = game.Workspace
 
local goal = {}
goal.Position = Vector3.new(0, 0, 0)
 
local tweenInfo = TweenInfo.new(3)
 
local tween = TweenService:Create(part, tweenInfo, goal)

tween.Completed:Connect(function(playbackState)
	if playbackState == Enum.PlaybackState.Completed then
		local explosion = Instance.new("Explosion")
		explosion.Position = part.Position 
		explosion.Parent = game.Workspace 
		part:Destroy()
		Debris:AddItem(explosion, 2)
	end
end)
 
tween:Play()

Tween Completed

In this example the walkspeed of any player joining the game will be slowed to 0 over 10 seconds using a Tween. The Completed event is used to reset the walkspeed after the Tween has finished playing.

local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
 
local SLOW_DURATION = 10
 
local function slowCharacter(humanoid)
	local goal = {}
	goal.WalkSpeed = 0
 
	local tweenInfo = TweenInfo.new(SLOW_DURATION)	
 
	local tweenSpeed = TweenService:Create(humanoid, tweenInfo, goal)
 
	tweenSpeed:Play()
 
	return tweenSpeed
end
 
local function onCharacterAdded(character)
	local humanoid = character:WaitForChild("Humanoid")
	local initialSpeed = humanoid.WalkSpeed
	local tweenSpeed = slowCharacter(humanoid)
	tweenSpeed.Completed:Wait()
	humanoid.WalkSpeed = initialSpeed
end
 
local function onPlayerAdded(player)
	player.CharacterAdded:Connect(onCharacterAdded)
end
 
Players.PlayerAdded:Connect(onPlayerAdded)