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

Verifying a Tween has Completed

Verifying a Tween has Completed

Oct 01 2018, 2:30 AM PST

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