Codectionary / Developer documentation / Luau

TweenService and transitions

TweenService interpolates supported properties over time. Creating a tween does not start it; call Play. Completion callbacks can also occur after cancellation, so inspect the playback state before treating the animation as a successful finish.

Syntax

local tween = TweenService:Create(instance, TweenInfo.new(1), {Transparency = 1})
tween:Play()

Examples

Fade a temporary marker

Script in ServerScriptService. The example creates an anchored part, fades it and destroys it only after successful completion. It does not use a per-frame loop.

local TweenService = game:GetService("TweenService")
local marker = Instance.new("Part")
marker.Anchored = true
marker.Position = Vector3.new(0, 5, 0)
marker.Parent = workspace
local tween = TweenService:Create(marker, TweenInfo.new(2), {Transparency = 1})
tween.Completed:Once(function(state)
    if state == Enum.PlaybackState.Completed then marker:Destroy() end
end)
tween:Play()

Cancel a transition deliberately

Alternative Script in ServerScriptService. Cancel stops this move after roughly half a second. The part stays at its current interpolated position; cancellation does not automatically reset its starting state.

local TweenService = game:GetService("TweenService")
local part = Instance.new("Part")
part.Anchored = true
part.Parent = workspace
local tween = TweenService:Create(part, TweenInfo.new(3), {Position = Vector3.new(0, 15, 0)})
tween.Completed:Once(function(state) print(state.Name) end)
tween:Play()
task.delay(0.5, function() tween:Cancel() end)

Best practices

  • Keep a reference to an active tween when a new interaction may cancel it.
  • Do not use animation completion as proof that a player earned an authoritative reward.
  • Choose short, readable transitions; constant movement can make a UI harder to use.

At a glance

Purpose
Typed scripting and Roblox development
File extension
.luau
Runs in
Luau host; Roblox engine examples require Roblox Studio
Usually used with
Roblox APIs and Studio

Specifications & further reading

Related Luau documentation