Cant Destroy Animation Tracks or Set Them to nil

I know there is a limit of 256x animation tracks a humanoid can load so i try to destroy the old loaded animations.
I have a tool that loads an animation when the parent switches to a parent of a humanoid(when its being equipped by a player or NPC).

When i set the tool to nil and then set it back to the same NPC then it checks if that same humanoid has the animations loaded and if the animation exists then it destroys them.

the issue is that when i call the destroy function on the animation track it wont get destroyed.

local tool = script.Parent
local isEquipped = false
local a = nil

function init()
	print(3)
	if tool.Parent and tool.Parent:FindFirstChildOfClass("Humanoid") and tool.Parent.Humanoid.Health > 0 and tool.Parent.Humanoid:FindFirstChildOfClass("Animator") then
		local anims = tool.Parent.Humanoid:FindFirstChildOfClass("Animator"):GetPlayingAnimationTracks()
		if #anims > 0 then
			print(#anims)
			for i,v in ipairs(anims) do
				v:Destroy()
				v = nil
			end
		end
		isEquipped = true
		if a == nil then
			a = tool.Parent.Humanoid.Animator:LoadAnimation(script.Parent.testAnim)
			a:Play()
		end
	
	end
end
init()
script.Parent.AncestryChanged:Connect(function(this,newParent)
	init()
end)

it keeps adding up animation instead of destroying them

You have to fire :Destroy on them to unload an animation track

So i have a gun that i want an NPC to equip and unequip the gun. The script that is inside the tool looks like this:

local tool = script.Parent
local isEquipped = false
local a,b,c = nil,nil,nil

function init()
	if tool.Parent and tool.Parent:FindFirstChildOfClass("Humanoid") and tool.Parent.Humanoid.Health > 0 and tool.Parent.Humanoid:FindFirstChildOfClass("Animator") then
		isEquipped = true
		if a == nil then
			a = tool.Parent.Humanoid.Animator:LoadAnimation(script.Parent.Aim)

		end
		if b == nil then
			b = tool.Parent.Humanoid.Animator:LoadAnimation(script.Parent.Fire)

		end
		if c == nil then
			c = tool.Parent.Humanoid.Animator:LoadAnimation(script.Parent.reload)

		end
		a:Play(0.1)
		print(2)
	end
end
init()
script.Parent.AncestryChanged:Connect(function(this,newParent)
	init()
end)

now when i test it on another script to see if the gun will load animations again if the parent is nil. the reason i set the parent to nil is because humanoid:UnequipTools() will move the tool to a nil parent.

local a = workspace.Tool
while wait() do
	a.Parent = workspace.NPC
	wait()
	a.Parent =  nil
end

i keep getting the warning of “AnimationTrack limit of 256 tracks for one Animator exceeded, new animations will not be played.”

1 Like

It might be a bug so i will post it on bug reports