Hello there, I am having some issues with the use of PathFindingService to direct an NPC to a desired target. I do apologize if this is a simple fix, I am not too versed with PFS yet (this being one of the first times I’ve attempted to use it).
It seems as though every time I execute this function I received the same error in the output.
local function pfToTarget(npc, target)
local humanoid = npc.Humanoid
if not humanoid then return end
local hrp = npc.HumanoidRootPart
if not hrp then return end
if not target:IsA("BasePart") then return end
local path = game:GetService("PathfindingService"):CreatePath()
local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection
local success, errorMessage = pcall(function()
path:ComputeAsync(hrp.Position, target.Position)
end)
if success and path.Status == Enum.PathStatus.Success then
waypoints = path:GetWaypoints()
blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
if blockedWaypointIndex >= nextWaypointIndex then
blockedConnection:Disconnect()
followPath(target.Position)
end
end)
if not reachedConnection then
reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
if reached and nextWaypointIndex < #waypoints then
nextWaypointIndex += 1
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
reachedConnection:Disconnect()
blockedConnection:Disconnect()
end
end)
end
nextWaypointIndex = 2
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
warn("Path not computed!", errorMessage)
end
end
Output:
15:30:56.792 Path not computed! nil - Server - NPCs:90
Given I am receiving a nil output, it suggests the ErrorMessage was not provided when computing the path; this is where I get confused.
you can try this snippet of code to update that with these changes made:
check for obstacles
verify target position
use waitforchild
debugging output
path status check and error handling
here
local function pfToTarget(npc, target)
local humanoid = npc.Humanoid
if not humanoid then return end
local hrp = npc.HumanoidRootPart
if not hrp then return end
if not target:IsA("BasePart") then return end
local path = game:GetService("PathfindingService"):CreatePath()
local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection
local success, errorMessage = pcall(function()
path:ComputeAsync(hrp.Position, target.Position)
end)
-- Debugging output
print("HRP Position:", hrp.Position)
print("Target Position:", target.Position)
print("Path Status:", path.Status)
if success then
if path.Status == Enum.PathStatus.Success then
-- Existing code for path success
else
warn("Path computation failed with status:", path.Status)
end
else
warn("Path computation encountered an error:", errorMessage)
end
end
With this edition, I received the following output. What are the exact circumstances for a “NoPath” status?
17:39:32.706 Path computation failed with status: Enum.PathStatus.NoPath - Server - NPCs:96
local pathfindingService = game:GetService("PathfindingService")
local humanoid = script.Parent:FindFirstChildOfClass("Humanoid")
local targetPosition = Vector3.new(-570, 4.5, -354.5) -- Example target position
local function followPath(targetPos)
local path = pathfindingService:CreatePath({
AgentRadius = humanoid.HipWidth,
AgentHeight = humanoid.HipHeight,
AgentCanJump = humanoid.Jump,
AgentJumpHeight = humanoid.JumpHeight,
AgentMaxSlope = humanoid.MaxSlope,
AgentWalkableFloorTypes = {Enum.Material.Fabric, Enum.Material.Plastic, Enum.Material.SmoothPlastic, Enum.Material.WoodPlanks},
AgentAvoidance = true,
ExcludeWater = true,
})
path:ComputeAsync(script.Parent.Position, targetPos)
if path.Status == Enum.PathStatus.Complete then
local waypoints = path:GetWaypoints()
local nextWaypointIndex = 2
local blockedConnection
local reachedConnection
local function moveToNextWaypoint()
if nextWaypointIndex <= #waypoints then
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
if reachedConnection then
reachedConnection:Disconnect()
end
if blockedConnection then
blockedConnection:Disconnect()
end
end
end
blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
if blockedWaypointIndex >= nextWaypointIndex then
blockedConnection:Disconnect()
if reachedConnection then
reachedConnection:Disconnect()
end
followPath(targetPos)
end
end)
if not reachedConnection then
reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
if reached then
nextWaypointIndex += 1
moveToNextWaypoint()
end
end)
end
moveToNextWaypoint()
else
warn("Path not computed!", path.Status)
end
end
followPath(targetPosition)