Skip to content

bdidk235/FunctionTween

Repository files navigation

FunctionTween

Tween whatever you want, however you want.

Roblox Model | Wally | GitHub Release

What else?

Tweens interpolate a number from start to end over a defined time, and there are better solutions for other problems.

For example, Spring is great for physics-based movement.

Usage

A simple example for moving a part, using PivotTo.

local FunctionTween = require(path.to.FunctionTween)

local PartToMove = workspace.PartToMove

local StartingPivot = PartToMove:GetPivot()
local GoalPivot = StartingPivot * CFrame.new(0, 0, 10)

local tween = FunctionTween.new(function(t)
	PartToMove:PivotTo(StartingPivot:Lerp(GoalPivot, t))
end, TweenInfo.new(1))
tween.Play(0, 1)
API Reference

FunctionTween.new

Creates a new FunctionTween table.

local RunService = game:GetService("RunService")

local FunctionTween = require(path.to.FunctionTween)

local easingFunction = function(x: number)
	return x ^ 2
end

local tween = FunctionTween.new(
	-- The function to tween
	function(t)
		PartToMove:PivotTo(StartingPivot:Lerp(GoalPivot, t))
	end,
	-- Can be a `TweenInfo` or a table with `TweenInfo` and `EasingFunction`
	{
		TweenInfo = TweenInfo.new(1),
		EasingFunction = easingFunction
	},
	-- What to update the function on (Based on MethodTween)
	RunService.Stepped
)

New FunctionTween

Functions

tween.Play

Plays the tween.

tween.Play(
	-- Start number
	0,
	-- End number
	1,
	-- Can be extended like `.new`'s TweenInfo
	TweenInfo.new(1)
)

tween.Pause

Pauses the tween.

tween.Pause()

tween.Resume

Resumes the tween after it was paused.

tween.Resume()

tween.Cancel

Cancels the tween, it cannot be resumed.

tween.Cancel()

Properties

tween.PlaybackState

The playback state of the tween.

tween.Completed

Fires when the tween is completed.

tween.Play(0, 1)
tween.Completed:Wait()
print("Tween completed!")

Other Functions

FunctionTween.LerpValue

Returns a value between valueA and valueB based on alpha, used internally for Equivalents.

print(FunctionTween.LerpValue(0, 100, 0.5)) -- Prints 50

Equivalents

FunctionTween.InstanceProps

Equivalent to TweenService:Create

local PartToMove = workspace.PartToMove

local tweenFunc = FunctionTween.InstanceProps(
	PartToMove,
	{ CFrame = PartToMove.CFrame * CFrame.new(0, 0, 10) }
)
local tween = FunctionTween.new(tweenFunc)

FunctionTween.InstanceMethods

Equivalent to MethodTween.new

local PartToMove = workspace.PartToMove

local StartingPivot = PartToMove:GetPivot()
local GoalPivot = StartingPivot * CFrame.new(0, 0, 10)

local tweenFunc = FunctionTween.InstanceMethods(
	workspace.PartToMove,
	{ PivotTo = { StartingPivot, GoalPivot } }
)
local tween = FunctionTween.new(tweenFunc)

Contributing

You will need Rokit (or Aftman) to install tools.

You can rojo serve serve.project.json to a Baseplate for Tests in Workspace, and Packages in ReplicatedStorage.

About

Tween whatever you want, however you want.

Resources

License

Stars

Watchers

Forks