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
Events, connections and cleanup
Connect registers a callback and returns a connection you can disconnect. The callback runs when the event fires, not when it is registered. Long-lived services can keep callbacks alive after a feature has ended, so give each connection a clear cleanup point.GUI buttons and user input
Player UI belongs on the client. Use GuiButton.Activated for a button that works across supported input methods, and avoid handling a keyboard shortcut when the interface has already consumed that input. A local UI action can request a server action, but cannot authorize it.task scheduling and RunService
Use task scheduling for delayed or deferred work, and RunService when a feature genuinely depends on simulation or rendering steps. A delay is a minimum scheduling interval rather than an exact clock. Avoid doing work every frame when an event can report the change.Raycasting and hit results
A raycast asks what a line segment intersects. Its direction vector includes the distance to search, not just a unit direction. A miss returns nil; a hit provides an Instance, position and surface normal. Filters determine what the query ignores.
Connect registers a callback and returns a connection you can disconnect. The callback runs when the event fires, not when it is registered. Long-lived services can keep callbacks alive after a feature has ended, so give each connection a clear cleanup point.GUI buttons and user input
Player UI belongs on the client. Use GuiButton.Activated for a button that works across supported input methods, and avoid handling a keyboard shortcut when the interface has already consumed that input. A local UI action can request a server action, but cannot authorize it.task scheduling and RunService
Use task scheduling for delayed or deferred work, and RunService when a feature genuinely depends on simulation or rendering steps. A delay is a minimum scheduling interval rather than an exact clock. Avoid doing work every frame when an event can report the change.Raycasting and hit results
A raycast asks what a line segment intersects. Its direction vector includes the distance to search, not just a unit direction. A miss returns nil; a hit provides an Instance, position and surface normal. Filters determine what the query ignores.