Help with AI kill script

I don’t have much experience with programming AI. To make this AI I used a model and modified it a bit, mostly customization, and I wanted the enemy to take away 50 health when it hits you, but that completely broke it. I believe it’s because the AI is still in the loop when it hits you, and it keeps duplicating the path. Here’s both my kill script and AI script, along with a video showcasing the issue.

AI

local TeddyAI = script.Parent

local event = game:GetService("ReplicatedStorage").Monster.E1
local event2 = game:GetService("ReplicatedStorage").Monster.E1Despawn

local Model = script.Parent
local Humanoid = Model:WaitForChild("Humanoid")
local RootPart = Model:WaitForChild("HumanoidRootPart")
local Head = Model:WaitForChild("Head")
local hipHeight = script.Parent.Humanoid.HipHeight

local Animations = Model:WaitForChild("Animations")
local Attack = Animations:WaitForChild("Slash")
local AttackAnimation = Humanoid:LoadAnimation(Attack)
AttackAnimation.Priority = Enum.AnimationPriority.Action

local SpawnAnim = Animations:WaitForChild("Spawn")
local SpawnAnimation = Humanoid:LoadAnimation(SpawnAnim)
SpawnAnimation.Priority = Enum.AnimationPriority.Action

local Sounds = Model:WaitForChild("Sounds")
local SpawnAudio = Sounds:WaitForChild("Spawn")
local AttackAudio = Sounds:WaitForChild("Attack")
local BellowAudio = Sounds:WaitForChild("Bellow")

local chasing = false
local followingTarget = nil
local SimplePath = require(game.ServerStorage.SimplePath)
local path = nil
local reached = false
local reached2 = false
local hipHeight = script.Parent.Humanoid.HipHeight

local pathParams = {
	["AgentHeight"] = ((hipHeight > 0 and hipHeight) or 4),
	["AgentRadius"] = script.Parent.HumanoidRootPart.Size.X,
	["AgentCanJump"] = false
}

script.Parent.HumanoidRootPart:SetNetworkOwner(nil)

local function getHumPos()
	return (TeddyAI.HumanoidRootPart.Position - Vector3.new(0,hipHeight,0))
end

local function displayPath(waypoints)
	local color = BrickColor.Random()
	for index, waypoint in pairs(waypoints) do
		local part = Instance.new("Part")
		part.BrickColor = color
		part.Anchored = true
		part.CanCollide = false
		part.Size = Vector3.new(1,1,1)
		part.Position = waypoint.Position
		part.Parent = workspace
		local Debris = game:GetService("Debris")
		Debris:AddItem(part, 6)
	end
end


local function findPotentialTarget()
	local players = game.Players:GetPlayers()
	local maxDistance = 40
	local nearestTarget

	for index, player in pairs(players) do
		if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
			if player.Character.Humanoid.Health > 0 then
				local target = player.Character
				local distance = (TeddyAI.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

				if distance < maxDistance then
					nearestTarget = player
					maxDistance = distance
				end
			end
		end
	end

	return nearestTarget
end

local function canSeeTarget(target)
	if target and target:FindFirstChild("HumanoidRootPart") then
		local origin = TeddyAI.HumanoidRootPart.Position
		local direction = (target.HumanoidRootPart.Position - TeddyAI.HumanoidRootPart.Position).unit * 10000
		local ray = Ray.new(origin, direction)
		local ignoreList = {TeddyAI}

		local hit, pos = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)

		-- check if it exists
		if hit then
			-- check if it hit
			if hit:IsDescendantOf(target) then
				-- check health
				if target.Humanoid.Health > 0 then
					-- check if target is safe or not
					if not game.Players:GetPlayerFromCharacter(target).Safe.Value then
						-- check if monster can see
						local unit = (target.HumanoidRootPart.Position - getHumPos()).Unit
						local lv = TeddyAI.HumanoidRootPart.CFrame.LookVector
						local dp = unit:Dot(lv)

						if dp > 0 then
							return true
						end		
					end			
				end
			end
		else
			return false
		end	
	end
end

local function getPath(destination)
	local PathfindingService = game:GetService("PathfindingService")

	local path = PathfindingService:CreatePath(pathParams)

	path:ComputeAsync(getHumPos(), destination.Position)

	return path
end

local function blockToBlock()
	if path and not reached then
		coroutine.wrap(function()
			path.Reached:Wait()
			reached2 = true
			print("Set")
		end)()
		repeat wait() if path == nil then break end until reached2 == true
	end
	reached = false
	reached2 = false
	path = nil
	coroutine.wrap(function()
		wait(1)
		if followingTarget and chasing == false then
			-- disable stuff for all targets
			print("disable things for all targets")
			for i, v in pairs(game.Players:GetPlayers()) do
				if v.GettingChasedBy.Value == script.Parent then
					v.GettingChased.Value = false
					v.GettingChasedBy.Value = nil
				end
			end
			TeddyAI.Chasing.Value = false
			followingTarget = nil
		end
	end)()
	print("now going to path")
	local goal = workspace.LoopPoints:GetChildren()[Random.new():NextInteger(1,#workspace.LoopPoints:GetChildren())]
	local path = getPath(goal)
	if path.Status == Enum.PathStatus.Success then
		--displayPath(path:GetWaypoints())
		for i, v in pairs(path:GetWaypoints()) do
			if findPotentialTarget() then
				if canSeeTarget(findPotentialTarget().Character) then
					break
				end
			end
			TeddyAI.Humanoid:MoveTo(v.Position)
			TeddyAI.Humanoid.MoveToFinished:Wait()
		end
	else
		print("nope")
	end
end

TeddyAI.Chasing.Changed:Connect(function()
	print(TeddyAI.Chasing.Value)
end)

script.Parent.Teleport.Event:Connect(function()
	path = nil
end)

local function SpawnMonsta()
	SpawnAudio:Play()
	SpawnAnimation:Play()
	SpawnAnimation.Stopped:Wait()
	script.Parent.Animator.Disabled = false
	
	while true do
		local target = findPotentialTarget()
		if target and canSeeTarget(target.Character) and target.Character.Humanoid.Health ~= 0 then
			print("found player")
			path = SimplePath.new(script.Parent,pathParams)
			path.Visualize = true
			local connection = path.Reached:Connect(function()
				reached = true
				script.Parent.Reached:Fire(target)
				print(path.Status)
			end)
			
			repeat
				chasing = true
				followingTarget = target
				-- enable stuff for target
				target.GettingChased.Value = true
				target.GettingChasedBy.Value = script.Parent
				TeddyAI.Chasing.Value = true
				path:Run(target.Character.HumanoidRootPart.Position)
				
			until not path or path.LastError == "ComputationError" or not target.Character or target.Character:FindFirstChild("HumanoidRootPart") == nil or target.Character.Humanoid.Health < 1 or target.Safe.Value or findPotentialTarget() ~= target or reached
			
			if connection then
				connection:Disconnect()
			end
			
			if findPotentialTarget() ~= target then
				if path and path._moveConnection then
					path:Stop()
				end
			end
		else
			blockToBlock()
		end
		game:GetService("RunService").Heartbeat:Wait()
	end
end

SpawnMonsta()

KILL SCRIPT

local teddy = script.Parent
local humanoid = teddy.Humanoid

local Animations = teddy:WaitForChild("Animations")
local Attack = Animations:WaitForChild("Slash")
local AttackAnimation = humanoid:LoadAnimation(Attack)
AttackAnimation.Priority = Enum.AnimationPriority.Action

local function getHumPos()
	return (teddy.HumanoidRootPart.Position)
end

script.Parent.Reached.Event:Connect(function(target)
	if target.Character then
		if target.Character.Humanoid.Health ~= 0 and target.Character ~= teddy then
			if target.Character.Humanoid.Health < 100 then
				AttackAnimation:Play()
				script.Parent.Sounds.Attack:Play()
				script.Parent.Sounds.Crunch:Play()
				target.Character.Humanoid.Health = 0
				AttackAnimation.Stopped:Wait()
				print('killed')
			else
				script.Parent.Humanoid.WalkSpeed = 0
				target.Character.Humanoid.Health -= 50
				AttackAnimation:Play()
				script.Parent.Sounds.Attack:Play()
				script.Parent.Sounds.Crunch:Play()	
				game:GetService("ReplicatedStorage"):WaitForChild("BumpCam"):FireClient(target)
				AttackAnimation.Stopped:Wait()
				script.Parent.Humanoid.WalkSpeed = 7
			end
		end
	end
end)