AnimationModule keeps returning "Animation doesn't exist"

Hi, I am using an Animation Module in my game which loads animations and plays them, but the problem is once I load the animation, and play it, it returns the “Animation doesn’t exist” error, even though it also prints it has successfully loaded the animation.

Module script:

local AnimatorModule = {};
AnimatorModule.__index = AnimatorModule

_G.Animators = _G.Animators or {};

function AnimatorModule.new(Animator)
	local self = setmetatable({}, AnimatorModule)
	self.Animator = Animator
	self.Animations = {}

	_G.Animators[Animator] = self

	Animator.Parent.AncestryChanged:Connect(function(p)
		if not p then
			_G.Animators[Animator] = nil
		end
	end)

	return self
end


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

function AnimatorModule:InsertAnimation(Animation, AName)
	local Name = AName 

	if typeof(Animation) == "Instance" then
		self.Animations[Name] = self.Animator:LoadAnimation(Animation)
	else
		return warn("Animation needs to be an instance!")
	end

	_G.Animators[self.Animator] = self
end


function AnimatorModule:Play(AnimationName, Speed, Weight, FadeTime)
	Speed = Speed or 1
	Weight = Weight or 1
	FadeTime = FadeTime or 0.1

	if not self.Animator then return end
	
	local animatorModule = _G.Animators[self.Animator]
	
	if animatorModule then
		local animation = animatorModule.Animations[AnimationName]
		if animation then
			if not animation.IsPlaying then
				animation:Play(FadeTime, Weight, Speed)
			end
		else
			warn("Animation '" .. AnimationName .. "' doesn't exist in the module's Animations table")
		end
	else
		warn("Animator module doesn't exist for the given Animator")
	end
end
-- shortened down

Client script:

local TackleX = Animations.new(Animator)
local AnimationToPlay

local success, err = pcall(function()
    TackleX:InsertAnimation(Animation, "T_X")
end)

if success then
    AnimationToPlay = Animations.GetAnimator(Animator)
end

AnimationToPlay:Play("T_X") -- line which causes the warning that i have placed in the module

Any help would be appreciated, thanks!

1 Like