darraghd493

Basic UI Animation Manager

Oct 29th, 2025 (edited)
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.63 KB | None | 0 0
  1. --[[
  2.     Basic UI Animation Manager by darraghd493
  3.  
  4.     A basic management library for UI libraries.
  5. ]]
  6.  
  7. local TweenService = game:GetService("TweenService")
  8.  
  9. local UIAnimationManager = {}
  10. UIAnimationManager.ActiveTweens = {}
  11.  
  12. function UIAnimationManager:Play(instance: Instance, tweenInfo: TweenInfo, properties: table, tag: string?)
  13.     assert(instance, "Instance is required")
  14.     assert(tweenInfo, "TweenInfo is required")
  15.     assert(properties, "Properties table is required")
  16.  
  17.     self:Cancel(instance, tag)
  18.  
  19.     local tween = TweenService:Create(instance, tweenInfo, properties)
  20.     local tweenTag = tag or "default"
  21.  
  22.     self.ActiveTweens[instance] = self.ActiveTweens[instance] or {}
  23.     self.ActiveTweens[instance][tweenTag] = tween
  24.  
  25.     tween.Completed:Connect(function()
  26.         if self.ActiveTweens[instance] then
  27.             self.ActiveTweens[instance][tweenTag] = nil
  28.             if next(self.ActiveTweens[instance]) == nil then
  29.                 self.ActiveTweens[instance] = nil
  30.             end
  31.         end
  32.     end)
  33.  
  34.     tween:Play()
  35.     return tween
  36. end
  37.  
  38. function UIAnimationManager:Cancel(instance: Instance, tag: string?)
  39.     local instanceTweens = self.ActiveTweens[instance]
  40.     if not instanceTweens then return end
  41.  
  42.     if tag then
  43.         local tween = instanceTweens[tag]
  44.         if tween then
  45.             tween:Cancel()
  46.             instanceTweens[tag] = nil
  47.         end
  48.     else
  49.         for key, tween in pairs(instanceTweens) do
  50.             tween:Cancel()
  51.             instanceTweens[key] = nil
  52.         end
  53.     end
  54.  
  55.     if next(instanceTweens) == nil then
  56.         self.ActiveTweens[instance] = nil
  57.     end
  58. end
  59.  
  60. function UIAnimationManager:CancelAll()
  61.     for instance, _ in pairs(self.ActiveTweens) do
  62.         self:Cancel(instance)
  63.     end
  64. end
  65.  
  66. return UIAnimationManager
Advertisement
Add Comment
Please, Sign In to add comment