Tweening¶
Tweens let you animate properties over time instead of changing them instantly. You create a tween with Tween:Create, then call :Play() to start it.
Tween:Create takes three things:
- the target — the object you want to animate
- an info table — options like how long it takes
- a goals table — which properties to animate and their target values
1. Moving a Part¶
local part = script.Parent
local tween = Tween:Create(part, { Time = 2 }, {
Position = part.Position + Vector3.New(0, 10, 0)
})
tween:Play()
tween.Finished:Wait()
print("Done!")
This moves the part up by 10 units over 2 seconds. Time is how long the tween takes, in seconds.
2. Animating Several Properties at Once¶
The goals table can hold as many properties as you want — they all animate together.
local part = script.Parent
local tween = Tween:Create(part, { Time = 1.5 }, {
Position = part.Position + Vector3.New(0, 5, 0),
Size = Vector3.New(4, 4, 4),
Color = Color.New(0, 1, 0)
})
tween:Play()
3. Styles and Direction¶
Use Style and Direction in the info table to change how the animation eases.
local part = script.Parent
local tween = Tween:Create(part, {
Time = 1,
Style = "bounce",
Direction = "out"
}, {
Position = part.Position + Vector3.New(0, 8, 0)
})
tween:Play()
Styles: linear, sine, quint, quart, quad, expo, elastic, cubic, circ, bounce, back, spring
Directions: in, out, inout, outin
4. Delay, Reverse and Repeat¶
local part = script.Parent
local tween = Tween:Create(part, {
Time = 1,
DelayTime = 0.5, -- wait half a second before starting
Reverses = true, -- animate back to the start afterwards
RepeatCount = 3 -- repeat 3 times (-1 for infinite)
}, {
Position = part.Position + Vector3.New(0, 6, 0)
})
tween:Play()
5. Reacting When It Finishes¶
Every tween has a Finished event and a Cancel method.
local part = script.Parent
local tween = Tween:Create(part, { Time = 2 }, {
Color = Color.New(1, 0, 0)
})
tween.Finished:Connect(function()
print("The part is now red!")
end)
tween:Play()
You can stop a running tween early with tween:Cancel().