Something is wrong with my chasing system

Hi there
I have some problems that my script doesn’t work. Here is my script

--{{Service}}--
local PathFindingService = game:GetService("PathfindingService")
local Players = game:GetService("Players")
--{{Model}}--
local NPC = game.Workspace:FindFirstChild("NPC")
local NPCRootPart = NPC:FindFirstChild("HumanoidRootPart")
--{{Distance}}--
local Distance
local ClosestDistance = math.huge
local ClosestPlayer
--{{Loop}}--
while task.wait() do
	--{{Find the closest player and closest distance}}--
	for _,player in ipairs(Players:GetPlayers()) do
		--{{character}}
		local character = player.Character
		local PrimaryPart = character:FindFirstChild("HumanoidRootPart")
		--{{Distance Update}}--
		Distance = (NPCRootPart.Position - PrimaryPart.Position).Magnitude

		if Distance < ClosestDistance then
			ClosestDistance = Distance
			ClosestPlayer = character
		end
	end
	--{{Path}}--
	local path = PathFindingService:CreatePath()
	--{{ComputeAsync}}--
	local success, error = pcall(function()
		path:ComputeAsync(NPCRootPart.Position, ClosestPlayer.PrimaryPart:GetPivot().Position)
	end)
	--{{Move to waypoints}}--
	for _,waypoint in ipairs(path:GetWaypoints()) do
		if success and path.Status == Enum.PathStatus.Success then
			NPC.Humanoid:MoveTo(waypoint)
		else
			warn(error)
		end
	end
end

Can someone fix it?

We need to know what problems you encounter and what you want to achieve with this script.

It’s just warn at the line have “HumanoidRootPart” variable


Local PrimaryPart = character:FindFirstChild(“HumanoidRootPart”)

About what I want to achieve, I want my NPC chasing nearest player by using PathFindingService

you should have checked if the character existed

local character = player.Character
if not character then
continue
end

move for _,waypoint inside if success and path status thingy

if character:FindFirstChild(“HumanoidRootPart”) doesnt work try using

character.PrimaryPart

@Dammio_6 use this simple chase nearest player script Instead if none of what i said works

local NPC = game.Workspace:FindFirstChild("NPC")
local runservice = game:GetService("RunService")

local function Closest()
	local range = math.huge
	local target = nil
	
	for _,player in pairs(game.Players:GetPlayers()) do
		local character = player.Character or player.CharacterAdded:Wait()
		if character then
			local distance = (NPC.PrimaryPart.Position - character.PrimaryPart.Position).Magnitude
			if distance < range then
				range = distance
				target = character
			end
		end
	end
	return target
end

runservice.Heartbeat:Connect(function()
	local target = Closest()
	if target then
		NPC.Humanoid:MoveTo(target.PrimaryPart.Position)
	end
end)

It uses direct moveto

If you need or wanna make your npc chase you by moving along waypoints, ask a programmer how to move rig to current waypoint on movetofinished:Once

Try instead of

for _,player in ipairs(Players:GetPlayers()) do

remove ipairs and do

for _, player in Players:GetPlayers() do

I don’t think you should go through players in strict sequence.

Thanks for your response. Anyway, I have seen that code somewhere before (but I don’t remember does it work)

the script works normal because i have already used it multiple times and i made it myself

I tried it and it worked, thanks!