BézierTween
Used to create BézierTweens which interpolate, or tween, the properties of instances using Bézier curves, allowing for more complex motion compared to regular Tweens. It uses nearly the same syntax as TweenService and uses similar methods to Tweens.
Showcase
Summary
Properties
Instance: Instance
TweenInfo: TweenInfo
PropertyTable: { [string]: {TweenableValue} }
PlaybackState: Enum.PlaybackState
tValue: NumberValue
ArcLengthTables: { [string]: number }
ConnectedTween: Tween?
tValueConnection: RBXScriptConnection?
DestroyConnection: RBXScriptConnection?
PlaybackConnection: RBXScriptConnection?
Methods
Create
Creates a new BezierTween given the object whose properties are to be tweened, a TweenInfo, and a dictionary of goal property values.
BezierTween:Create(
instance: Instance, tweenInfo: TweenInfo, propertyTable: { [string]: {TweenableValue} }
) -> BezierTween
Play
Starts playback of a BezierTween. Note that if playback has already started, calling Play() has no effect unless the tween has finished or is stopped (either by BezierTween:Pause() or BezierTween:Cancel())
BezierTween:Play() -> BezierTween
Pause
Halts playback of the BezierTween. Doesn’t reset its progress variables, meaning that if you call BezierTween:Play(), the BezierTween resumes playback from the moment it was paused.
BezierTween:Pause() -> BezierTween
Cancel
Halts playback and resets the BezierTween variables. If you then call BezierTween:Play(), the properties of the BezierTween resume interpolating towards their destination, but take the full length of the animation to do so.
BezierTween:Cancel() -> BezierTween
Destroy
Destroys a BezierTween, its associated variables, and its associated NumberValue.
BezierTween:Destroy() -> ()
Reparameterize
Reparameterizes a BezierTween by arclength to make it animate at constant speed before easing is applied. Should be called before :Play(). Defaults to 100 samples if not specified.
BezierTween:Reparameterize(samples: number?) -> BezierTween
DestroyOnCompleted
Destroys a BezierTween, its associated variables, and its associated NumberValue automatically upon completion.
BezierTween:DestroyOnCompleted() -> BezierTween
Events
Completed
Fires when the tween finishes playing or when stopped with BezierTween:Cancel().
BezierTween.Completed: RBXScriptSignal
Tweens are integrated within BézierTweens to take advantage of their features. Since the module is written this way, all parameters of TweenInfos are compatible with BézierTweens (easing style, easing direction, repeat count, looping, etc.).
BézierTweens can be used on any object with compatible property types, including:
Unlike Tweens which may be fully cleared by the garbage collector, BézierTweens require a NumberValue to exist for functionality. This will be destroyed upon destroying a BézierTween. BézierTweens should be destroyed when they are finished being used to prevent memory leaks.
Code Samples
BézierTween Creation
-- Require the module
local BezierTween = require(PATH_HERE.BezierTween)
-- Create a part to be animated
local part = Instance.new("Part")
part.Anchored = true
part.Parent = workspace
-- TweenInfo
local tweenInfo = TweenInfo.new(
5, -- Time
Enum.EasingStyle.Sine,
Enum.EasingDirection.Out,
4, -- Repeat count
true, -- Reverses
0 -- Delay time
)
-- Create the property table for the BézierTween to follow
local propertyTable = {
CFrame = {
CFrame.new(5, 5, 5, 2, 1, 2, 1),
CFrame.new(9, 2, 5, 1, 1, 0, 4),
CFrame.new(2, 4, 6, 2, 0, 4, 2)
},
Color = {
Color3.new(1, 0.2, 1),
Color3.new(0.2, 0, 1),
Color3.new(0.5, 1, 0),
Color3.new(0, 0.8, 1),
},
Reflectance = {
0,
0.5
}
} -- Each property can have a different number of points
-- Create the BézierTween
BezierTween:Create(part, tweenInfo, propertyTable)
:Play()
:DestroyOnCompleted() -- Clean up when finished
Pausing, Resuming, and Cancelling
-- Require the module
local BezierTween = require(PATH_HERE.BezierTween)
-- Create a part to be animated
local part = Instance.new("Part")
part.Anchored = true
part.Parent = workspace
-- Create a BezierTween
local partTween = BezierTween:Create(
part,
TweenInfo.new(5, Enum.EasingStyle.Sine),
{
Size = {
Vector3.new(0.5, 5, 0.2),
Vector3.new(2, 2, 2),
Vector3.new(0.5, 5, 0.2)
}
}
)
partTween:Play()
task.wait(3)
partTween:Pause() -- Halts progress
task.wait(1)
partTween:Play() -- Plays from where it was paused after Pause()
task.wait(0.5)
partTween:Cancel() -- Cancels and restarts progress
partTween:Play():DestroyOnCompleted() -- Plays from beginning after Cancel()
Reparameterization
-- Require the module
local BezierTween = require(PATH_HERE.BezierTween)
-- Create a part to be animated
local part = Instance.new("Part")
part.Anchored = true
part.Parent = workspace
-- Create a BezierTween
local partTween = BezierTween:Create(
part,
TweenInfo.new(5, Enum.EasingStyle.Linear),
{
Position = {
Vector3.new(5, 5, 15),
Vector3.new(2, 8, 2),
Vector3.new(4, 5, 7)
}
}
)
:Reparameterize(100) -- Reparameterize by arclength using 100 samples.
:Play() -- Will animate with constant speed since it has been reparameterized and uses linear easing.
-- Linear easing without reparameterizing may animate at varying speed due to varying rates of change per unit of t along the curve.
:DestroyOnCompleted()
Performance Note
Although this module has some computational performance optimizations (De Casteljau’s algorithm), BézierTweens have more performance impact than regular Tweens. I recommend limiting to under ~200 BézierTweens playing at once. This value will vary based on the number of points in each Bézier curve. More points mean more calculations.
Similarly to regular Tweens, using BézierTweens on the server should be avoided unless for good reason.
