How do i fix AnimationTrack limit of 256 tracks for one Animator exceeded, new animations will not be played

  1. What do you want to achieve? I wanna play the anim attack left and right if it is close to player and damage them

  2. What is the issue? i get a error saying AnimationTrack limit of 256 tracks for one Animator exceeded, new animations will not be played. (x408) - Studio causing MASSIVE LAG

  3. What solutions have you tried so far? i looked on dev forum but i didnt understand and dont know if they could help with my issue

here’s the script (idk how to fix it because im still learning)

local Rig = script.Parent
local Humanoid = Rig:WaitForChild("Humanoid")
local RootPart = Rig:WaitForChild("HumanoidRootPart")
local ScreamSound = Rig:WaitForChild("ScreamSound")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local WanderRange = 100000
local DetectionRange = 60
local AttackRange = 5

local IsScreaming = false
local IsChasing = false

local WanderSpeed = 15.5
local ScreamSpeed = 0
local ChaseSpeed = 25

local IsWandering

local WanderDelay = Rig.Config.WanderDelay.Value

local AttackAnimations = {
	Left = {
		Animation = Rig:WaitForChild("AttackAnimationLeft"),
		Damage = 5,
	},
	Right = {
		Animation = Rig:WaitForChild("AttackAnimationRight"),
		Damage = 7,
	}
}

local Animator = Humanoid:FindFirstChildOfClass("Animator") or Instance.new("Animator", Humanoid)

local function getNearestLowestHealthPlayer()
	local nearestPlayer = nil
	local lowestHealth = math.huge
	local closestDistance = math.huge

	for _, player in pairs(Players:GetPlayers()) do
		if player.Character and player.Character:FindFirstChild("Humanoid") then
			local character = player.Character
			local playerHumanoid = character:FindFirstChild("Humanoid")
			local distance = (RootPart.Position - character.HumanoidRootPart.Position).magnitude

			if distance < DetectionRange and playerHumanoid.Health < lowestHealth then
				nearestPlayer = player
				lowestHealth = playerHumanoid.Health
				closestDistance = distance
			end
		end
	end

	return nearestPlayer, closestDistance
end

local function wander()
	local randomDirection = Vector3.new(math.random(-1, 1), 0, math.random(-1, 1)).unit
	local destination = RootPart.Position + randomDirection * WanderRange
	Humanoid:MoveTo(destination)
	Humanoid.WalkSpeed = WanderSpeed
end

local function avoidObstacles()
	local ray = Ray.new(RootPart.Position, RootPart.CFrame.LookVector * 5)
	local hit = workspace:FindPartOnRay(ray, Rig)

	if hit then
		RootPart.CFrame = RootPart.CFrame * CFrame.Angles(0, math.rad(90), 0)
	end
end

local function screamAndChase(player)
	if not IsScreaming and not IsChasing then
		IsScreaming = true
		Humanoid.WalkSpeed = ScreamSpeed
		ScreamSound:Play()
		Animator:LoadAnimation(script:WaitForChild("ScreamAnimation")):Play()

		wait(Rig.Config.ScreamDuration.Value)

		IsScreaming = false
		IsChasing = true

		Humanoid.WalkSpeed = ChaseSpeed

		local playerCharacter = player.Character
		local playerHumanoid = playerCharacter:FindFirstChild("Humanoid")

		while playerHumanoid and playerHumanoid.Health > 0 and (RootPart.Position - playerCharacter.HumanoidRootPart.Position).magnitude < DetectionRange do
			Humanoid:MoveTo(playerCharacter.HumanoidRootPart.Position)
			wait(0.1)
		end

		IsChasing = false
		Humanoid.WalkSpeed = WanderSpeed
	end
end

local function attackPlayer(player)
	if (RootPart.Position - player.Character.HumanoidRootPart.Position).magnitude < AttackRange then
		local randomAttack = math.random(2) == 1 and "Left" or "Right"
		local attackAnimInfo = AttackAnimations[randomAttack]
		local attackAnim = Animator:LoadAnimation(attackAnimInfo.Animation)

		attackAnim:Play()

		local connection
		connection = attackAnim:GetMarkerReachedSignal("DamagePoint"):Connect(function()
			if (RootPart.Position - player.Character.HumanoidRootPart.Position).magnitude < AttackRange then
				player.Character.Humanoid:TakeDamage(attackAnimInfo.Damage)
			end
		end)

		attackAnim.Stopped:Connect(function()
			connection:Disconnect()
			attackAnim:Destroy()
		end)

		wait(attackAnim.Length)
	end
end

RunService.Stepped:Connect(function()
	if not IsChasing and not IsScreaming then
		avoidObstacles()

		if not IsWandering then
			IsWandering = true
			wander()
			delay(WanderDelay, function()
				IsWandering = false
			end)
		end
	end

	local nearestPlayer, distance = getNearestLowestHealthPlayer()
	if nearestPlayer and distance < DetectionRange then
		screamAndChase(nearestPlayer)
	end

	if nearestPlayer and distance < AttackRange then
		attackPlayer(nearestPlayer)
	end
end)
1 Like

The problem is caused by you loading the animation every time the attack happens. You should load the animation only once when necessary.

The other option is to use a Module which loads all animations on to the characters which is then called to Play the animations when required. There is an example one somewhere on the DevForum I have seen:

1 Like

so basically, just load the animations and put them in a table, then play the stored animation, or use the module