Path not computed! Nil

I am trying to make NPCs spawn and go to random positions.

I am using PathFindingService to make NPCs go to a random position from table.

local PathFindingService = game:GetService("PathfindingService")



local points = workspace.Points

local pathPoints = {
	["blacksmith"] = points.Blacksmith.Position,
	["armorsmith"] = points.armorsmith.Position,
	["merchant"] = points.merchant.Position,
	["robuxshop"] = points.robuxshop.Position
	
}

local pathPointsTable = {"blacksmith", "armorsmith", "merchant", "robuxshop"}

local randompointPos = pathPoints[pathPointsTable[math.random(#pathPointsTable)]]

print(randompointPos)


local human = script.Parent:WaitForChild("Humanoid")
local humanoidrootpart = script.Parent:WaitForChild("HumanoidRootPart")

local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection


local path = PathFindingService:CreatePath()

local function followPath(destination)
	-- Compute the path
	local success, errorMessage = pcall(function()
		path:ComputeAsync(humanoidrootpart.Position, destination)
	end)

	if success and path.Status == Enum.PathStatus.Success then
		-- Get the path waypoints
		waypoints = path:GetWaypoints()

		-- Detect if path becomes blocked
		blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
			-- Check if the obstacle is further down the path
			if blockedWaypointIndex >= nextWaypointIndex then
				-- Stop detecting path blockage until path is re-computed
				blockedConnection:Disconnect()
				-- Call function to re-compute new path
				followPath(destination)
			end
		end)

		-- Detect when movement to next waypoint is complete
		if not reachedConnection then
			reachedConnection = human.MoveToFinished:Connect(function(reached)
				if reached and nextWaypointIndex < #waypoints then
					-- Increase waypoint index and move to next waypoint
					nextWaypointIndex += 1
					human:MoveTo(waypoints[nextWaypointIndex].Position)
				else
					reachedConnection:Disconnect()
					blockedConnection:Disconnect()
				end
			end)
		end

		-- Initially move to second waypoint (first waypoint is path start; skip it)
		nextWaypointIndex = 2
		human:MoveTo(waypoints[nextWaypointIndex].Position)
	else
		warn("Path not computed!", errorMessage)
	end
end

wait(5)

followPath(randompointPos)

What I get is Path not computed! nil and I don’t know what causes it. I tried even using one dummy but even in workspace it just doesn’t move. I don’t know if it’s a problem with the random position from the table or something else. Help much appreciated :smiley:

does printing the status print nil?

It prints
Enum.PathStatus.NoPath

1 Like