kAnimator is a simple object that loads and plays animations on-demand.
Why use this?
- Add/remove animations waiting to be loaded at any time
- Get and play animations without worrying about character states
- Avoid messy code handling cases where animations aren’t loaded/don’t exist yet
Example usage (A punch tool):
local kAnimator = require( [path.to.kAnimator] )
local Player = game:GetService("Players").LocalPlayer
local AnimationsFolder = [path.to.some.anims]
local Loader = kAnimator.new(Player, AnimationsFolder) -- Add a folder of animations ready
local function OnClicked()
Loader:Get("PunchAnimation"):Play() -- Quickly get and play animation
end
local function OnSynchronizedAction()
Loader:Add( [path.to.fancyanim] ) -- Adds animation during runtime
local Track = Loader:WaitUntilLoaded("FancyAnimation") -- Waits until animation is ready to play immediately
-- start some effects here
Track:Play()
Track.Stopped:Wait()
-- end the effects here
Loader:Remove("FancyAnimation)
end
local function ToolDeleted()
Loader:Destroy() -- Cleans all animations, connections and instances
end
Most important API:
-
kAnimator.new( player: Player, animations: Folder | {[string]: Animation}) → kAnimator
Creates a new kAnimator object for that player. Can be ran on both server and client with no differences. -
kAnimator:Get( name: string ) → AnimationTrack
Returns an AnimationTrack if available. If no animation by that name was added, or the character isn’t ready, it will return a “fake” AnimationTrack that has non-functional methods. This makes it easy to lazily ignore cases where animations don’t exist!
To check if an AnimationTrack is fake or not:if Track.Animation then -- Do stuff else -- fake animation! end
-
kAnimator:WaitUntilLoaded( name: string ) → AnimationTrack
Same behavior as :Get(), except yields until AnimationTrack.Length is greater than 0. Times out after 5 seconds by default. -
kAnimator:Add( animation(s): Animation | Folder | {[string]: Animation})
Provide a single animation, a folder or a table of animations to add to the object. The name of the animation stored is the name of the Animation instance, or the key name if a table is provided.
Check it out here:
This is an extended module from the one I created for Maldives Resorts! We have encountered zero errors relating to this module so far, and it has worked extremely reliable for a stable playerbase.