NPC randomly stops after a few seconds

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!
    Please help me make an npc chase a player without stopping
  2. What is the issue? Include screenshots / videos if possible!
    the npc chases player but after a few seconds, it stops walking
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried adding cooldowns when paths are created but it still doesn’t work
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

npc idle.rbxl (146.5 KB)
Also, this npc uses noobpath module script

local function getNearestTarget(npc, root)
	local dist = 1000
	local char

	
		for _, player in pairs(game.Players:GetPlayers()) do
			local vChar = player.Character
			if vChar and vChar:FindFirstChild("Humanoid") and vChar:FindFirstChild("HumanoidRootPart") then
				local vHum = vChar:WaitForChild("Humanoid")
				local vRoot = vChar:WaitForChild("HumanoidRootPart")
				if vHum.Health > 0 then
					local magnitude = (vRoot.Position - root.Position).Magnitude
					if magnitude <= dist then
						dist = magnitude
						char = vChar
					end
				end
			end
		end
	return char
end

local npc = script.Parent

local ServerStorage = game:GetService("ServerStorage")
local NoobPath = require(ServerStorage.NoobPath)
local Path = require(ServerStorage.NoobPath.Path)

-- Define AgentParams for the NPC
local AgentParams = {
	AgentRadius = 2,  -- Example value
	AgentHeight = 2,  -- Example value
	AgentCanJump = false,
	AgentJumpHeight = 7,
	AgentMaxSlope = 45,
	Costs = {warning = 1}
}

-- Initialize NoobPath with AgentParams
local Guy, JumpDetectConnection = NoobPath.Humanoid(npc, AgentParams)

local thing = Path.new()

local designatedAreasFolder = workspace:WaitForChild("ai_zones")
local designatedParts = {}

-- Collect designated area parts
for _, part in ipairs(designatedAreasFolder:GetChildren()) do
	if part:IsA("BasePart") then
		table.insert(designatedParts, part)
	end
end

npc.HumanoidRootPart:SetNetworkOwner(nil) -- change character to the variable that your npc is in.

Guy.Reached:Connect(function(Waypoint, IsPartial)
	task.wait(0.1)
	Guy:Run()
end)

Guy.WaypointReached:Connect(function(Waypoint, NextWaypoint)
	Guy:Run()
end)

Guy.Error:Connect(function(ErrorType : string)
	task.wait(0.1)
	Guy:Run()
end)

Guy.Trapped:Connect(function(TrapType : string)
	if npc.Hunt:GetState() ~= Enum.HumanoidStateType.Climbing then -- Climbing is slower than usual
		Guy.Jump() -- Jump to unstuck
	end

	Guy:Run()
end)

-- Function to check if NPC is inside any designated area part
local function isInsideAnyPart(character)
	for _, part in ipairs(designatedParts) do
		local partPos = part.Position
		local partSize = part.Size
		local characterPos = character.HumanoidRootPart.Position

		-- Check if the NPC is within the area of the part
		local xRange = math.abs(characterPos.X - partPos.X) <= partSize.X / 2
		local yRange = math.abs(characterPos.Y - partPos.Y) <= partSize.Y / 2
		local zRange = math.abs(characterPos.Z - partPos.Z) <= partSize.Z / 2

		if xRange and yRange and zRange then
			return true
		end
	end
	return false
end

Guy.Visualize = true
JumpDetectConnection:Disconnect()

game:GetService("RunService").Heartbeat:Connect(function()
	local target = getNearestTarget(npc, npc.HumanoidRootPart)

	if target then
		local targetRoot = target:WaitForChild("HumanoidRootPart")

		-- Check if NPC is inside any designated area
		if isInsideAnyPart(npc) then
			-- Run towards target within designated areas
			Guy:Run(targetRoot)
			--thing.Generate()
			if Guy.Idle then

				if npc.Hunt:GetState() ~= Enum.HumanoidStateType.Running then -- Climbing is slower than usual
					Guy.Jump() -- Jump to unstuck
				end

				Guy:Run()
			end

		else
			-- Move directly to the target outside designated areas
			Guy:Stop()

			npc.Hunt:MoveTo(targetRoot.Position)
		end

	end
end)