Skip to content

Tween

RoStrap's premier Tween Module built for Roblox. Allows you to write interpolation code faster with a clear and simple API.

Declaration

First let's load the module:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Resources = require(ReplicatedStorage:WaitForChild("Resources"))

local Tween = Resources:LoadLibrary("Tween")
local Enumeration = Resources:LoadLibrary("Enumeration")
Once you've loaded the Tween Module, there are two ways to create a Tween. You can either interpolate a property of an object, or create a custom Tween with a function you want called each Heartbeat frame.

Library API

Tween

TweenObject Tween(Object Object, string PropertyName, Variant EndValue, Enumeration.EasingFunction EasingFunction, number Time, bool Override = false, function(TweenStatus) Callback = nil, [arg InitialParameter])

Tween function for tweening any property. Like GuiObject:TweenPosition() but the first two arguments are Object and Property. If InitialParameter isn't nil, it will be pushed to the first argument passed to the Callback.

-- Localizing the `.Value` of an EasingFunction is fastest
local OutQuad = Enumeration.EasingFunction.OutQuad.Value
local Standard = Enumeration.EasingFunction.Standard.Value

Tween(workspace.Part, "CFrame", CFrame.new(10, 10, 10), OutQuad, 2, true)
Tween(workspace.Part, "Transparency", 1, Standard, 2, true)

Override works a bit differently in this system than in Roblox's. Roblox's override parameter should be named "Overridable" whereas this parameter is whether the new Tween should override any previous open Tweens. The same things can be accomplished with both, but this is a notable difference.

Tween.new

TweenObject Tween.new(number Duration, string EasingFunctionName, function Callback, [arg InitialParameter])

Tweens created with Tween.new will call Callback every tween Heartbeat frame, with EasingFunction interpolating from 0 to 1 over the allotted duration. If InitialParameter isn't nil, it will be pushed to the first argument passed to the Callback.

Tween.new(number Duration, string EasingFunctionName, function Callback)

local Deceleration = Enumeration.EasingFunction.Deceleration.Value

local newTween = Tween.new(0.5, Deceleration, function(x)
    print("This will be called with each 'Frame' of this tween")
end)

TweenObject API

Tween:Resume

TweenObject Tween:Resume()

Resumes a Tween that was Stop()ed

Tween:Stop

TweenObject Tween:Stop()

Stops a Tween

Tween:Wait

TweenObject Tween:Wait()

Yields until Tween finishes interpolating

Tween:Restart

TweenObject Tween:Restart()

Makes the Tween go back to timeElapsed = 0

Fields

Field Description Type Default value
Running Whether the Tween is active bool true