Pathfinding Returning Nil

Hey all!

Complete beginner with pathfinding here. I’ve created a system that spawns in rigs, and am attempting to make a function that pathfinds the NPCs to a particular point. As it stands, the function I wrote is returning an error of “Nil”. I am not sure what I am doing wrong. Any advice is appreciated!

local PathfindingService = game:GetService("PathfindingService")
local Rig = game.ServerStorage.Rig
local SpawnPart = game.Workspace.SpawnPart
local Words = require(game.ServerScriptService.Words)

local function InitializePath (EnemyNPC)
	local humanoid = EnemyNPC:WaitForChild("Humanoid")
	local torso = EnemyNPC:WaitForChild("Torso")
	local path = PathfindingService:CreatePath()
	local success, errorMessage = pcall(function()
		path:ComputeAsync(torso.Position, - 136, torso.Position.Y, torso.Position.Z))
	if success and path.Status == Enum.PathStatus.Success then
		local waypoints = path:GetWaypoints()
		for i, waypoint in pairs(waypoints) do


-- function that spawns the "enemies"

local function SpawnEnemy(Word)
	local Enemy = Rig:Clone()
	local humanoid = Enemy:WaitForChild("Humanoid")
	local torso = Enemy:WaitForChild("Torso")
	local ZCoordinate = math.random((SpawnPart.Position.Z - SpawnPart.Size.X/2), (SpawnPart.Position.Z + SpawnPart.Size.X/2))
	Enemy.Name = ""
	Enemy.Parent = game.Workspace
	Enemy:MoveTo(, SpawnPart.Position.Y + 10, ZCoordinate))


1 Like

Could you send the error message please?

The only thing I have in the console is “nil” printed by the errorMessage

Erm instead of;

local torso = Enemy:WaitForChild("Torso")

you should change it to

local HumanoidRootPart = Enemy:WaitForChild("HumanoidRootPart")

since the HumanoidRootPart is basically the PrimaryPart and has all the connections.

1 Like

Good catch! Even after changing that, I am still getting the same errorMessage though, with no movement from the npc that spawned in

maybe cause the pcall is a success but not the path status, try printing the path status on the else statement if errorMessage is nil

1 Like

What if you try

local PrimaryPart = Enemy.PrimaryPart

The PrimaryPart should be the Head I think.

That was a money idea! It returned NoPath.

Any ideas of how I could fix it? The NPCs spawn along the transparent green line seen on the bottom. Their goal is their X position, minus 134 studs, which is meant to bring them right up against the grey wall. The closest black part, which has collisions enabled, has a pathfinding modifier with passthrough set to true, while the others are cancollide false.

And I realized that my “minus” sign should’ve been a plus. Problem solved, pathfinding working!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.