NPC won't move and I'm not sure why

Hey there,

To make this really simple, this script won’t move the NPC. The NPC just stands there. He is unanchored. There are no errors in the output.

local pathFindingService = game:GetService('PathfindingService')

local human = script.Parent:WaitForChild('Humanoid')
local torso = script.Parent:WaitForChild('UpperTorso')

local path = pathFindingService:CreatePath()
path:ComputeAsync(torso.Position, game.Workspace.EndPart.Position)
local waypoints = path:GetWaypoints()

for i, waypoint in pairs(waypoints) do
	human:MoveTo(waypoint.Position)
end

human:MoveTo(game.Workspace.EndPart.Position)

Thanks for any help you can offer. If you need any more help, just let me know :slight_smile: .

I would check the path status first… For example:

local function CreatePath(Start, End)
	local Success, Path = pcall(function()
		local Path
		Path = PathfindingService:CreatePath()
		Path:ComputeAsync(Start, End)
		return Path
	end)

	if not Success or Path.Status ~= Enum.PathStatus.Success then
		Path = {
			GetWaypoints = function()
				return {{Position = End}}
			end
		}
	end
	return Path
end

And also, when moving from one waypoint to another, you should totally use Humanoid.MoveToFinished:Wait()!

Path = CreatePath(torso.Position, OTHER_POSITION_HERE)
Path = Path:GetWaypoints()
for i,v in next, Path do
    Humanoid:MoveTo(v.Position)
    Humanoid.MoveToFinished:Wait()
end

Let me know if it works.

Hm, nothing appears to be happening :frowning:

I have been having issues with Roblox’s Pathfinding service for awhile, and recently had to create a custom solution using A* pathfinding as a backup, in cases in which Roblox’s pathfinding fails - I have it as a fail safe backup.

What I think is happening is in the cases of a large map, the pathfinding mesh the Roblox system creates is not completed in time, by the time I need pathfinding to work. I figured this out when I noticed the initial minutes of my story game would have to use my backup system, but the Roblox pathfinding system would start working, minutes later.

The roblox pathfinding worked fine for months on my main game map, but at one point just stopped working , initially for one scene of my game, but would work later on for other scenes (its a story game).

I hope this helps give you some clues on what’s going on… I’m not sure how big your map is, but again, I suspect its the special mesh that’s created, that’s not completing in time, in the area of the map you need it to work…

1 Like

its been a year later. I’m having this same problem but ON A REALLY SMALL MAP. Smaller than default baseplate. Oddest thing is the ouput is computing a successful path (Enum.PathStatus.Success) but the npc is not moving at all. MoveToFinished:Wait() somehow creates an infinite yield because nothing prints past that line of code.

2 Likes