AI Pathfinding breaks after a player gets killed

Hi, I’m having an issue whenever the pathfinding AI usually kills the player, if the AI detects another player, it will do two things, stop little then move little repeated (SetNetworkOwner has been set to nil)
or it would chase player, but then just randomly stop. i do have a stuck script so if AI hasn’t moved in next 4s it would teleport back to spawn, but it would just do same thing.

I’ve been stuck for hours trying to figure out the issue, sometimes using ChatGPT (didn’t work duh)
I’m not an expert at coding, so you may find some of my coding out of hand, I did take an
already done AI script and modified it

AI script:

local Pathfinding = game:GetService("PathfindingService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local db = false
local dbtwo = false

local path = Pathfinding:CreatePath({
	AgentHeight = 6;
	AgentRadius = 3;
	AgentCanJump = false;
	
	Cost = {
		Water = 100;
		DangerZone = math.huge
	}
})

local Character = script.Parent
local humanoid = Character:WaitForChild("Humanoid")

local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection

local function AttachHeadToSwordAndFace(player)
	if dbtwo then return false end
	dbtwo = true

	local npchum = script.Parent:FindFirstChild("Humanoid")
	local playerhum = player.Character and player.Character:FindFirstChild("Humanoid")
	local hrp = player.Character and player.Character:FindFirstChild("HumanoidRootPart")
	if not (npchum and playerhum and hrp) then 
		dbtwo = false
		return false
	end

	player.Character:PivotTo(script.Parent.Killoff.CFrame)

	local weld = Instance.new("WeldConstraint")
	weld.Part0 = hrp
	weld.Part1 = script.Parent.Killoff
	weld.Parent = hrp

	local npcAnim = npchum:LoadAnimation(script.Killoff)
	local playerAnim = playerhum:LoadAnimation(script.PlayerkilledOff)

	npcAnim:Play()
	playerAnim:Play()

	script.Parent.HumanoidRootPart["Stab Splat Shorter"]:Play()
	script.Parent.HumanoidRootPart["A Hat in Time B-Side  Jingle She Caught You "]:Play()

	task.wait(1.25)

	weld:Destroy()

	script.Parent.HumanoidRootPart["A Hat in Time B-Side  Jingle She Caught You "]:Stop()
	script.Parent.HumanoidRootPart["Death Sound"]:Play()
	script.Parent.HumanoidRootPart["Record scratch sound effect"]:Play()
	game.ReplicatedStorage.Remotes.Music:FireClient(player, game.ReplicatedStorage["Chase Themes"].Jimmy.Havoc, true)

	playerhum.Health = 0

	dbtwo = false
	return true
end

local function createHitbox(character)
	local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
	if not humanoidRootPart then return end

	local hitbox = Instance.new("Part")
	hitbox.Size = Vector3.new(7,7,7)
	hitbox.Transparency = 0
	hitbox.Material = Enum.Material.ForceField
	hitbox.Color = Color3.fromRGB(255, 0, 0)
	hitbox.Anchored = true
	hitbox.CanCollide = false
	hitbox.Name = "Hitbox"
	hitbox.CFrame = humanoidRootPart.CFrame * CFrame.new(0, 0, -2)
	hitbox.Parent = workspace

	local touchedPlayers = {}
	hitbox.Touched:Connect(function(hit)
		local hitChar = hit:FindFirstAncestorOfClass("Model")
		if hitChar and hitChar ~= character then
			local humanoid = hitChar:FindFirstChild("Humanoid")
			local playerHit = Players:GetPlayerFromCharacter(hitChar)

			if humanoid and playerHit and not touchedPlayers[playerHit] then

				if humanoid.Health <= 25 then
					if dbtwo then return end
					print(dbtwo)
					print(playerHit.Name)
					script.Parent.HumanoidRootPart.Anchored = true
					script.Parent.Humanoid.PlatformStand = true
					AttachHeadToSwordAndFace(playerHit, script.Parent)
					task.wait(1.45)
					script.Parent.HumanoidRootPart.Anchored = false
					script.Parent.Humanoid.PlatformStand = false
					db = false
					dbtwo = false
					return
				end

				touchedPlayers[playerHit] = true
				humanoid:TakeDamage(20)
				script["sword slash"]:Play()
			end
		end
	end)

	task.delay(0.5, function()
		hitbox:Destroy()
	end)
end

local function tryAttack(target)
	if target.Humanoid.Health <= 0 then return end
	if db then return end
	db = true

	script.Parent.Slash.Value = true
	createHitbox(script.Parent)
	script.Parent.Humanoid.WalkSpeed = 0
	script.Parent.Humanoid:LoadAnimation(script:WaitForChild("Slash")):Play()
	script["Sword Swing Metal Heavy"]:Play()

	task.wait(1)
	script.Parent.Humanoid.WalkSpeed = 22
	db = false
	script.Parent.Slash.Value = false
end

local function findTarget()
	local maxDistance = 235
	local nearestTarget
	
	for index, player in pairs(Players:GetPlayers()) do
		if player.Character then
			local target = player.Character
			local distance = (Character.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
			
			if distance < maxDistance then
				nearestTarget = target
				maxDistance = distance
			end
			
			if distance < 5 then
				if dbtwo then return end
				tryAttack(player.Character)
			end
		end
	end
	
	return nearestTarget
end

local function followPath(destination)
	if reachedConnection then
		reachedConnection:Disconnect()
		reachedConnection = nil
	end
	if blockedConnection then
		blockedConnection:Disconnect()
		blockedConnection = nil
	end

	local success, err = pcall(function()
		path:ComputeAsync(Character.PrimaryPart.Position, destination)
	end)

	if success and path.Status == Enum.PathStatus.Success then
		waypoints = path:GetWaypoints()
		nextWaypointIndex = 2

		blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
			if blockedWaypointIndex >= nextWaypointIndex then
				followPath(destination)
			end
		end)

		reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
			if reached then
				nextWaypointIndex += 1
				if nextWaypointIndex <= #waypoints then
					humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
				else
					reachedConnection:Disconnect()
					reachedConnection = nil
					blockedConnection:Disconnect()
					blockedConnection = nil
				end
			end
		end)

		if waypoints[nextWaypointIndex] then
			humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
		end
	else
		warn("Path not computed:", err)
	end
end

local currentTarget = nil
local lastTargetPos = nil
local repathDistance = 4
local repathDelay = 0.1 

while true do
	local target = findTarget()

	if target and target:FindFirstChild("HumanoidRootPart") then
		local targetPos = target.HumanoidRootPart.Position

		if not lastTargetPos or (targetPos - lastTargetPos).Magnitude >= repathDistance then
			lastTargetPos = targetPos
			currentTarget = target
			followPath(targetPos)
		end
	end

	task.wait(repathDelay)
end

if anyone could post down any solutions or issues that might be within the script, please let me know. I’ve been stuck on this one script for hours and its starting to really grind my gears.

Video: Uploading: 2025-07-27 16-51-59.mp4…

Screenshots:
Before killed, chasing


After killed, freezes, sometimes moves slow