Animation Track Invalid Or Nil

Recently for my game i have made an animation handler module, so far it worked perfectly, animations dont overlap, and no other major bugs, And still there IS no bug:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AnimationFolder = ReplicatedStorage:WaitForChild("Animations")
local AnimationHandler = {}
AnimationHandler.__index = AnimationHandler

function AnimationHandler.new(model)
	local self = setmetatable({}, AnimationHandler)
	self.Model = model

	
	local humanoid = model:FindFirstChildOfClass("Humanoid")
	if humanoid then
		self.Animator = humanoid:FindFirstChildWhichIsA("Animator")
		if not self.Animator then
			self.Animator = Instance.new("Animator")
			self.Animator.Parent = humanoid
		end
	else
		local animationController = model:FindFirstChild("AnimationController")
		if animationController then
			self.Animator = animationController:FindFirstChildWhichIsA("Animator")
			if not self.Animator then
				self.Animator = Instance.new("Animator")
				self.Animator.Parent = animationController
			end
		else
			warn("No Humanoid or AnimationController found in model: " .. model.Name)
		end
	end

	self.AnimationTracks = {}

	for _, anim in ipairs(AnimationFolder:FindFirstChild(model:GetAttribute("titan_Type")):GetChildren()) do
		if anim:IsA("Animation") then
			local track = self.Animator:LoadAnimation(anim)
			self.AnimationTracks[anim.Name] = track
		end
	end
	
	return self
end


function AnimationHandler:Play(name, fadeTime, callback)
	fadeTime = fadeTime or 0.5	
	local track = self.AnimationTracks[name]
	if track then
		track:Play(fadeTime)
		if callback then
			return callback(track)
		end
	else
		warn("Animation '" .. name .. "' not found!")
	end
end


function AnimationHandler:Stop(name, fadeTime)
	fadeTime = fadeTime or 0.5
	local track = self.AnimationTracks[name]
	if track then
		track:Stop(fadeTime)
	else
		warn("Animation '" .. name .. "' not found!")
	end
end


function AnimationHandler:StopAll(fadeTime)
	fadeTime = fadeTime or 0.5
	for _, track in pairs(self.AnimationTracks) do
		track:Stop(fadeTime)
	end
end

return AnimationHandler

When i try to retrieve the current track in:

function AnimationHandler:Play(name, fadeTime, callback)
	fadeTime = fadeTime or 0.5	
	local track = self.AnimationTracks[name]
	if track then
		track:Play(fadeTime)
		if callback then
			return callback(track)
		end
	else
		warn("Animation '" .. name .. "' not found!")
	end
end

It just returns as nil when i try reference it in a script/localscript,
I have NO idea as to why this is happening, please do help me.

1 Like