Animation doesnt play on NPC due to animation limits

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

Animation plays on NPC

  1. What is the issue? Include screenshots / videos if possible!

It doesn’t work. It shows this error

AnimationTrack limit of 256 tracks for one Animator exceeded, new animations will not be played. (x1032) - Studio

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I already searched on social medias but none of them would solve my problem

If you wondering wheres the animation code

local Animator = humanoid:WaitForChild("Animator")
local WalkAnim = script:WaitForChild("Animation")
local AnimLoad = Animator:LoadAnimation(WalkAnim)

AnimLoad:Play()

local CollectionService = game:GetService("CollectionService")
local animId = 8724952905

game:GetService("RunService").Heartbeat:Connect(function()
	
	task.wait()
	for _, mob in pairs(CollectionService:GetTagged('mob')) do
		
		task.spawn(function()
			
			if #CollectionService:GetTags(mob) < 1 then return end
			
			local humanoid = mob:WaitForChild("Humanoid")
			local Torso = mob:WaitForChild("HumanoidRootPart")
			
			local dmg = mob.Settings.Damage.Value
			local cooldown = mob.Settings.AttackCooldown.Value
			local range = mob.Settings.AttackRange.Value
			local coolingDown = mob.Settings.CoolingDown
			
			mob.BillboardGui.Frame.TextLabel.Text = humanoid.Health .. "/" .. humanoid.MaxHealth
			
			local closestChar
			
			for _, player in pairs(game.Players:GetPlayers()) do
				
				local char = player.Character
				
				if char and char:FindFirstChild("HumanoidRootPart") and char.Humanoid.Health > 0 then
					
					local distance = (char.HumanoidRootPart.Position - Torso.Position).Magnitude
					
					if not closestChar then closestChar = char end
					
					local clocestDistance = (closestChar.HumanoidRootPart.Position - Torso.Position).Magnitude
					
					if distance < clocestDistance then
						closestChar = char
					end
				end
			end
			
			if closestChar then 
				
				humanoid:MoveTo(closestChar.HumanoidRootPart.Position)
				
				local Animator = humanoid:WaitForChild("Animator")

				local WalkAnim = script:WaitForChild("Animation")
				local AnimLoad = Animator:LoadAnimation(WalkAnim)

				AnimLoad:Play()
				
				if not coolingDown.Value then
					
					local ray = Ray.new(Torso.Position, Torso.CFrame.LookVector * range)
					
					local part = workspace:FindPartOnRay(ray, workspace:WaitForChild("MobsFolder"))
					
					if part then
						
						local player = game.Players:GetPlayerFromCharacter(part.Parent) or game.Players:GetPlayerFromCharacter(part.Parent.Parent)
						
						if player then
							
							coolingDown.Value = true
							
							player.Character.Humanoid:TakeDamage(dmg)
							task.wait(cooldown)
							coolingDown.Value = false
						end
					end
				end
			end
		end)
	end
end)

1 Like

Why are you playing an animation inside of an event which fires every frame?

local CollectionService = game:GetService("CollectionService")
local animId = 8724952905
local AnimLoad

game:GetService("RunService").Heartbeat:Connect(function()

	task.wait()
	for _, mob in pairs(CollectionService:GetTagged('mob')) do

		task.spawn(function()

			if #CollectionService:GetTags(mob) < 1 then return end

			local humanoid = mob:WaitForChild("Humanoid")
			local Torso = mob:WaitForChild("HumanoidRootPart")

			local dmg = mob.Settings.Damage.Value
			local cooldown = mob.Settings.AttackCooldown.Value
			local range = mob.Settings.AttackRange.Value
			local coolingDown = mob.Settings.CoolingDown

			mob.BillboardGui.Frame.TextLabel.Text = humanoid.Health .. "/" .. humanoid.MaxHealth

			local closestChar

			for _, player in pairs(game.Players:GetPlayers()) do

				local char = player.Character

				if char and char:FindFirstChild("HumanoidRootPart") and char.Humanoid.Health > 0 then

					local distance = (char.HumanoidRootPart.Position - Torso.Position).Magnitude

					if not closestChar then closestChar = char end

					local clocestDistance = (closestChar.HumanoidRootPart.Position - Torso.Position).Magnitude

					if distance < clocestDistance then
						closestChar = char
					end
				end
			end

			if closestChar then 

				humanoid:MoveTo(closestChar.HumanoidRootPart.Position)

				local Animator = humanoid:WaitForChild("Animator")

				local WalkAnim = script:WaitForChild("Animation")
				
				if not AnimLoad then
					AnimLoad = Animator:LoadAnimation(WalkAnim)
					repeat task.wait() until AnimLoad.Length > 0
				end
				
				if not AnimLoad.IsPlaying then
					AnimLoad:Play()
				end

				if not coolingDown.Value then

					local ray = Ray.new(Torso.Position, Torso.CFrame.LookVector * range)

					local part = workspace:FindPartOnRay(ray, workspace:WaitForChild("MobsFolder"))

					if part then

						local player = game.Players:GetPlayerFromCharacter(part.Parent) or game.Players:GetPlayerFromCharacter(part.Parent.Parent)

						if player then

							coolingDown.Value = true

							player.Character.Humanoid:TakeDamage(dmg)
							task.wait(cooldown)
							coolingDown.Value = false
						end
					end
				end
			end
		end)
	end
end)

You should move this part of the code outside of the .Heartbeat connection. You can only call :LoadAnimation 256 times before Roblox stops loading the animation.

Actually i took these code from a tutorial. But if i remove the heartbeat event, the code won’t work.

@Judgy_Oreo

I tried that before but not work

Just figured it out. I just need to make a new script and put it inside of npc and then plays the animation

Sorry for wasting replier’s times