AnimatorModule - Easily access animations from different scripts

I’ve created a small module for easy animation control between scripts. It allows inserting/playing/stopping animations from different scripts.

The module is easy to use and can be used on animator (humanoids and animation controllers to)


API :

AnimatorModule.new Creates new “Class” using Animator (Humanoid and Animation Controller works too).

AnimatorModule.new(Instance :: Animator || Humanoid || Animation Controller)

AnimatorModule.GetAnimator returns already existing animator

AnimatorModule.GetAnimator(Instance :: Animator || Humanoid || Animation Controller)

AnimatorModule:GetAnimatorList() returns table with all existing animators

AnimatorModule:GetAnimatorList()

Animator:Play - this requires string argument (given animation name) and plays the given animation. (if this is empty it will result an warning). Speed,Weight and FadeTime are optional

Animator:Play(AnimationName :: string, Speed :: number, Weight :: number, FadeTime :: number)

Animator:Stop This requires string argument (given animation name) and stops the given animation. (if this is empty it will result an warning)

Animator:Stop(AnimationName :: string)

Animator:StopAll Stops all animation

Animator:StopAll()

Animator:GetAnimationList() returns table with all animations names for the given animator instance

Animator:GetAnimationList()


Example:

-- Script #1
local AnimatorModule = require(game.ReplicatedStorage.AnimatorModule)

local Animator = AnimatorModule.new(Animator) -- creates instance to given Animator 
(humanoid and animation controller works too)
Animator:InsertAnimation(4123512626,"Epic Animation") -- this works with Animation instance or just the id/link to the animation.
wait(3)
Animator:Stop("Epic Animation") -- stops the animation

And here is the second script

-- Script #2
local AnimatorModule = require(game.ReplicatedStorage.AnimatorModule)

wait(1)
local Animator = AnimatorModule.GetAnimator(Animator) -- gets given Animator  
Animator:Play("Epic Animation") -- plays the animation from different script.

Here is the module: Animator Module

Here is the code:

local AnimatorModule = {};
AnimatorModule.__index = AnimatorModule

_G.Animators = _G.Animators or {};

function AnimatorModule.new(Animator : Animator)
	if game:GetService("RunService"):IsClient() then warn("Inserting animations for the client will be accessable only on this client. Be aware") end
	local self = setmetatable({},AnimatorModule);
	self.Animator = Animator;
	self.Animations = {};

	_G.Animators[Animator] = self

	Animator.Parent.AncestryChanged:Connect(function(p)
		if not p then
			table.remove(Animator,Animator);
		end
	end)

	return self;
end

function AnimatorModule.GetAnimator(Animator : Animator)
	return _G.Animators[Animator]
end

function AnimatorModule:InsertAnimation(Animation, Name : string)
	Name = Name or "Animation"..#self.Animations;

	if typeof(Animation) == "Instance" then
		self.Animations[Name] = self.Animator:LoadAnimation(Animation);
	else		
		local rbxasset = tonumber(Animation) and "rbxassetid://"..Animation or Animation;
		Animation = Instance.new("Animation");
		Animation.AnimationId = rbxasset;

		self.Animations[Name] = self.Animator:LoadAnimation(Animation);
	end

	_G.Animators[self.Animator] = self
end

function AnimatorModule:Play(AnimationName : string, Speed : number, Weight : number, FadeTime : number)
	Speed = Speed or 1;
	Weight = Weight or 1;
	FadeTime = FadeTime or 0.100000001;
	if not self.Animator then return end
	if _G.Animators[self.Animator].Animations[AnimationName] then
		if not _G.Animators[self.Animator].Animations[AnimationName].IsPlaying then
			_G.Animators[self.Animator].Animations[AnimationName]:Play(FadeTime,Weight,Speed);
		end
	else warn("Animation doesnt exist"); end
end

function AnimatorModule:Stop(AnimationName : string)
	if not self.Animator then return end
	if _G.Animators[self.Animator].Animations[AnimationName] then 
		if _G.Animators[self.Animator].Animations[AnimationName].IsPlaying then
			_G.Animators[self.Animator].Animations[AnimationName]:Stop(); 
		end
	else warn("Animation doesnt exist"); end
end

function AnimatorModule:StopAll()
	if not self then return end
	for _,v in pairs(_G.Animators[self.Animator].Animations) do
		v:Stop();
	end
end

function AnimatorModule:GetAnimationList()
	local anims = {};
	for Name,_ in pairs(self.Animations) do table.insert(anims,Name) end;
	return anims;
end

function AnimatorModule.GetAnimatorList()
	local animators = {};
	for _,tabel in pairs(_G.Animators) do 
		animators[#animators+1] = {FullName = tabel.Animator:GetFullName(), Instance = tabel.Animator}
	end;
	return animators;
end

return AnimatorModule;

P.S - this is just a fun little project of mine, feel free to edit the source. (The code is also pretty rushed xd)

17 Likes

Loading Animations into the Humanoid has since been deprecated with the introduction of the Animator Instance.

You can read more about this here

1 Like

it works with animator too, so there isn’t need of updating the module.

1 Like

Everytime i load the animations, then go to play them in another script is says animations does not exist

Thank you for this module it has proven extremely useful in our latest project. You go into the ever growing list of people that provide code resources that save us DevForum dwellers countless hours of frustration. Thanks once again.

1 Like

I love this, this is going to be very useful.
Thank you!

1 Like