Extreme bug in animation that i cant seem to fix

  1. What do you want to achieve? Keep it simple and clear!
    i want to fix an animation bug that i cant seem to see the problem

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

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    this only happened after i removed the animationcontroller of my npc, because before that it simply wouldnt play the animation
wait(1)
local function findnearesttarget()
	local maxdistance = 300
	local nearest = nil
	for i,target in game.Players:GetChildren() do
		if target.Character then
			if target.Character.Humanoid.Health <= 0 then
				
			else
				
				local distance = (target.Character:WaitForChild("HumanoidRootPart").Position - script.Parent.Head.Position).Magnitude
				if distance < maxdistance then
					nearest = target.Character
					maxdistance = distance
				end
			end
				


		end

	end
	return nearest
end
local anim = script.Parent.Humanoid.Animation
local animtrack = script.Parent.Humanoid:LoadAnimation(anim)

while wait() do
	script.Parent.char.Value = findnearesttarget()
	if findnearesttarget() and (findnearesttarget():WaitForChild("HumanoidRootPart").Position - script.Parent.Head.Position).Magnitude > 100 then
		if animtrack.IsPlaying == false then
			print("playtrack")
			animtrack:Play()
		end
		script.Parent.Humanoid:MoveTo(findnearesttarget():WaitForChild("HumanoidRootPart").Position)

	else
		script.Parent.Humanoid:MoveTo(script.Parent.HumanoidRootPart.Position)
		print("staph")
		animtrack:Stop()
	end

		


end