Animator Class to make scripting animations easier!

Hello, I made this in hopes that someone learning to script can utilize this to make working with animations a little easier. It’s a simple module.


Usage:

Animator.new takes the humanoid object and a folder of animations, in which is uses the children of that folder to load the animatons. It returns the new object.

Animator.new(instance Humanoid, instance AnimationsFolder)

Animator:play takes a string (the animation’s name) which would be the name of the animation in the folder and plays the animation. You can also optionally pass a second parameter called AnimationSpeed which uses the AdjustSpeed method for roblox animations. Default is 1.

Animator:play(string ‘Anim’, float AnimationSpeed)

Animator:stop Does the same as play() but stops the given animation.

Animator:stop(string ‘Anim’)

Animator:stopAll Takes no arguments and stops all current playing animations

Animator:stopAll()

Example:

local AnimatorClass = require(AnimatorModule);
local Animator = AnimatorClass.new(Humanoid, Folder)

Animator:play('Swing1')

Code:

local Animator = {
    animator = nil;
    humanoid = nil;

    Animations = {};
};

function Animator.new( Humanoid, Animations )
    assert( Humanoid and Animations );

    local self = setmetatable( {}, {__index = Animator} );

    self.animator = Humanoid.Animator;
    self.humanoid = Humanoid;

    for _,anim in pairs(Animations:GetChildren()) do
        if anim:IsA('Animation') then
            self.Animations[anim.Name] = self.animator:LoadAnimation( anim );
        end
    end

    return self;
end

function Animator:play( animName, animSpeed )
    assert( animName );

    local anim = self.Animations[animName];

    if ( anim ) then
        anim:Play();
        anim:AdjustSpeed(animSpeed or 1);
    end
end

function Animator:stop( animName )
    assert( animName );

    local anim = self.Animations[animName];

    if ( anim ) then
        anim:Stop();
    end
end

function Animator:stopAll()
    local anims = self.animator:GetPlayingAnimationTracks();

    for _,v in pairs(anims) do
        v:Stop();
    end
end

return Animator;
2 Likes

wow this is useful good job I would definitely use it!

2 Likes