Skip to content

Instantly share code, notes, and snippets.

@Accoast
Created January 7, 2024 20:36
Show Gist options
  • Select an option

  • Save Accoast/63ac382f29f13c67bb54ffc955498d19 to your computer and use it in GitHub Desktop.

Select an option

Save Accoast/63ac382f29f13c67bb54ffc955498d19 to your computer and use it in GitHub Desktop.
FastTween
function FastTween(objects: {Instance}, properties: {[string]: any}, easing: {number? | Enum.EasingStyle? | Enum.EasingDirection?}, delay: number?): Tween
-- Check if the first parameter is a table, if not, convert it to a table
if type(objects) ~= "table" then
objects = {objects}
end
-- Create an empty array to store Tween objects
local tweens: {Tween} = {}
-- Loop through each object in the input table
for _, obj in pairs(objects) do
-- Check if easing is nil, and set default values if necessary
if easing == nil then
easing = {1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out}
else
local secondArgument: any = easing[2]
-- Check and convert easing style to Enum
if secondArgument == nil then
easing[2] = Enum.EasingStyle.Sine
elseif type(secondArgument) == "string" then
if string.lower(secondArgument) == "expo" then
easing[2] = Enum.EasingStyle.Exponential
else
easing[2] = Enum.EasingStyle[secondArgument]
end
end
-- Check and convert easing direction to Enum
if easing[3] == nil then
easing[3] = Enum.EasingDirection.InOut
elseif type(easing[3]) == "string" then
easing[3] = Enum.EasingDirection[easing[3]]
end
end
-- Create Tween object using TweenService
local tweenInfo: TweenInfo = TweenInfo.new(unpack(easing))
local tween: Tween = game:GetService("TweenService"):Create(obj, tweenInfo, properties)
-- Define a coroutine to wait for delay and then play the Tween
task.defer(function()
if delay then
wait(delay)
end
tween:Play()
end)()
-- Insert the Tween object into the tweens array
table.insert(tweens, tween)
end
-- Return the Tween objects as individual values
return unpack(tweens)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment