Update tween.luau

fix test fail again! yes i'm a retard
This commit is contained in:
sindri 2026-03-03 13:53:49 +01:00 committed by GitHub
parent 8ca9e9b440
commit 4edaddf151
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,47 +1,62 @@
-- src/tween.luau
local TweenService = game and game:GetService("TweenService") or nil
local HasTweenInfo = (TweenInfo ~= nil)
local HasTweenInfo = (TweenInfo ~= nil)
--cleanup and action injection so tweens are stopped on scope destruction
-- Factory: inject action + cleanup so tweens are stopped on scope destruction
return function(action, cleanup)
local tween = {}
-- Presets (simple, useful)
-- Presets: only available when TweenInfo exists (Roblox runtime)
if HasTweenInfo then
tween.presets = {
instant = TweenInfo.new(0),
fast = TweenInfo.new(0.12, Enum.EasingStyle.Quad, Enum.EasingDirection.Out),
smooth = TweenInfo.new(0.22, Enum.EasingStyle.Quad, Enum.EasingDirection.Out),
snappy = TweenInfo.new(0.16, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out),
}
tween.presets = {
instant = TweenInfo.new(0),
fast = TweenInfo.new(0.12, Enum.EasingStyle.Quad, Enum.EasingDirection.Out),
smooth = TweenInfo.new(0.22, Enum.EasingStyle.Quad, Enum.EasingDirection.Out),
snappy = TweenInfo.new(0.16, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out),
}
else
tween.presets = {}
end
-- Imperative helper (for events): vide.tweenTo(instance, {Position=...}, info)
-- Imperative helper: plays a tween (or applies instantly in tests)
function tween.tweenTo(instance, goals, info)
if not instance or not goals then
return nil
end
-- Non-Roblox / unit tests: apply instantly
if not TweenService then
-- Non-Roblox / tests: just apply instantly
if instance and goals then
for k, v in pairs(goals) do
instance[k] = v
end
for k, v in pairs(goals) do
instance[k] = v
end
return nil
end
local tweenInfo = info or tween.presets.smooth
local tweenInfo = info
if tweenInfo == nil then
if tween.presets.smooth then
tweenInfo = tween.presets.smooth
else
-- Fallback in case presets are not available for any reason
tweenInfo = TweenInfo.new(0.22, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
end
end
local tw = TweenService:Create(instance, tweenInfo, goals)
tw:Play()
return tw
end
-- Action helper (for create trees):
-- create "Frame" { tween({Position=...}, presets.fast), ... }
-- Action helper: attach tweening in create() trees
function tween.tween(goals, info)
return action(function(instance)
if not goals then
return
end
-- If TweenService not available, apply instantly (tests outside of roblox environment)
-- Non-Roblox / unit tests: apply instantly
if not TweenService then
for k, v in pairs(goals) do
instance[k] = v
@ -49,11 +64,19 @@ return function(action, cleanup)
return
end
local tweenInfo = info or tween.presets.smooth
local tweenInfo = info
if tweenInfo == nil then
if tween.presets.smooth then
tweenInfo = tween.presets.smooth
else
tweenInfo = TweenInfo.new(0.22, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
end
end
local tw = TweenService:Create(instance, tweenInfo, goals)
tw:Play()
-- Stop on scope destruction to avoid leaking connections/tweens
-- Ensure tween is cancelled when the scope is destroyed
cleanup(function()
pcall(function()
tw:Cancel()