Animations break on death?

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!
    An animation for my pathfinding system
  2. What is the issue? Include screenshots / videos if possible!
    Whenever the ai kills the player, the animations for the ai do not reset back to their walking but whenever i make it so the rig wont kill the player, aka putting them on 1 hp it turns it back to the walking animation.

pretty long script but line 151 (where it states "print(“RAN)”) is where this issue is happening (the print statement does run)

local rs = game:GetService("ReplicatedStorage")
local ps = game:GetService("PathfindingService")

local pathfind = {}

local VISION_RANGE = 40
local ATTACK_RANGE = 15
local KILL_RANGE = 3.5
local target = nil
local navigating = false
local track
local destinationReached = nil
local db = false
	
	local function resetMonster(monster)
		monster.Humanoid.WalkSpeed = 8
		if track then
			db = false
			track:Stop()
		end
	end
	
	local function GetRandomPath(monster)
		local waypoints = workspace.PatrolPoints:GetChildren()
		local randomPoint = waypoints[math.random(#waypoints)]
		
		local path = ps:CreatePath({
			AgentRadius = 2,
			AgentHeight = 4,
			AgentCanJump = false,
			Costs = {
				noMonsterAllowed = math.huge
			},
		})
		
		local success, errorMessage = pcall(function()
			path:ComputeAsync(monster.PrimaryPart.Position, randomPoint.Position)
		end)
		
		if success and path.Status == Enum.PathStatus.Success then
			return path:GetWaypoints()
		else
			local reverse = (monster.PrimaryPart.CFrame * CFrame.new(0,0,3)).Position
			monster.Humanoid:MoveTo(reverse)
			task.wait(.1)
			return false
		end
	end
	
	local function endNavigation()
		navigating = false
		if destinationReached then
			destinationReached:Disconnect()
		end
	end
	
	local function startNavigation(monster)
		navigating = true
		local waypoints = GetRandomPath(monster)
		if not waypoints then
			endNavigation()
			return
		end
		
		local waypointIndex = 2
		destinationReached = monster.Humanoid.MoveToFinished:Connect(function(reached)
			if reached and waypointIndex < #waypoints then
				waypointIndex += 1
				monster.Humanoid:MoveTo(waypoints[waypointIndex].Position)
			else
				endNavigation()
			end
		end)
		monster.Humanoid:MoveTo(waypoints[waypointIndex].Position)
	end
	
	local function GetTarget(monster)
		local maxDistance = VISION_RANGE
		local newTarget = nil
		
		for i, player in game.Players:GetPlayers() do
			local char = player.Character
			if not char then continue end
			local root = char:FindFirstChild("HumanoidRootPart")
			local hum = char:FindFirstChild("Humanoid")
			if not root or not hum or hum.Health <= 0 then 
				resetMonster(monster)
			continue end
			
			local origin = monster.Head.Position
			local direction = root.Position - origin
			local distance = direction.Magnitude
			-- if farther away from vision then dont care
			if distance > VISION_RANGE then 
				resetMonster(monster)
			continue end
			
			if distance > ATTACK_RANGE then
				local fov = direction.Unit:Dot(monster.Head.CFrame.LookVector)
				-- if behind monster or not facing them.
				if fov < 0.5 then 
				continue end
			end
			
			local anim = monster.Animation 
			track = monster.Humanoid:LoadAnimation(anim)
			monster.Humanoid.WalkSpeed = 15
			if db == false then
				db = true
				track:Play()
			end
			
			
			local result = workspace:Raycast(origin, direction * VISION_RANGE)
			-- something goes wrong
			if not result or not result.Instance then 
				resetMonster(monster)
			continue end
			-- if hit a wall
			if not result.Instance:FindFirstAncestor(char.Name) then
				resetMonster(monster)
			continue end
			
			if distance < maxDistance then
				maxDistance = distance
				newTarget = char
			end
		end
		return newTarget, maxDistance
	end
	
	function pathfind.New()
		local monster = rs.Enemy.Skinwalker:Clone()
		local spawnPos = workspace.MonsterSpawnPos.CFrame * CFrame.new(0,0,0)
		monster:PivotTo(spawnPos)
		monster.Humanoid.WalkSpeed = 10
		monster.Parent = workspace
		monster.PrimaryPart:SetNetworkOwner(nil)
		
		local distance
		navigating = false
		
		task.spawn(function()
			while true do
				target, distance = GetTarget(monster)
				if target then
					monster.Humanoid:MoveTo(target.PrimaryPart.Position)
					if distance < KILL_RANGE then
						print("RAN")
						track:Stop()
						resetMonster(monster)
						target.Humanoid.Health = 0
					end
				elseif not navigating then
					db = false
					resetMonster(monster)
					
					startNavigation(monster)
				end
				
				task.wait()
			end
			
			endNavigation()
			monster:Destroy()
		end)
	end
return pathfind

Have you tried clearing the last target whenever the AI kills the player? That might be the issue.

I think of setting target.Humanoid.Health to zero before the resetMonster().

Or just make animations into locals and use them when needed and stop when needed

maxdistance is changed to distance’s value ?

I have tried that but the issue still persists for some reason, the walk speed resets to normal but the animation just keeps playing

This sadly still doesn’t work for some reason, the animate script inside of the rig is a server script but using roblox’s runcontext to run it as a client, this may cause the issue but I’m unsure because I’ve never messed with runcontext before, I have also found a solution which is to enable and disable the animate script even though it’s not the most practical.