If you want to script NPC Pathfinding on Roblox, it’s is actually very simple. In this blog, you’ll learn how to script NPC Pathfinding on Roblox in an easy way.
What is NPC Pathfinding?
NPC Pathfinding is when you have an NPC, and you’d like it to travel to a destination, avoiding objects along the way.
How to Make NPCs Walk in Roblox
To make NPCs walk to a certain destination, you can use the Humanoid:MoveTo function.

Let’s say that we want the Noob to travel to the SpawnLocation. How would we script this?
Well, it’s easy enough:
local noob = workspace.Noob
noob.Humanoid:MoveTo(workspace.SpawnLocation.Position)
In here, all we’re saying is “Let the Noob walk straight to the spawn location”

And it manages to walk straight to the SpawnLocation. Just as we wanted.
But what if there’s an object in the way?

What do you think will happen?

Well, the Noob gets stuck, because, well, there’s a giant red block in the way. In particular, when you use the MoveTo
function, Roblox just makes the Noob travel straight to its destination.
But we can do better.
How to Make NPCs Avoid Obstacles in Roblox using PathfindingService
To make NPCs avoid objects in Roblox, you’ll want to use something called PathfindingService.
PathfindingService has functions that do a whole bunch of calculations to get your NPC to a destination, while intelligently avoiding obstacles.
Earlier, when the NPC tried to go to the SpawnLocation, it got stuck! But, with PathfindingService, we can get past this.
We have to start by telling the PathfindingService that you want to create a new path.
local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath()
Here, we’ve told PathfindingService that we intend on creating a new path, and stored it in the variable “path.”
local success, errorMessage = pcall(function()
path:ComputeAsync(workspace.Noob.HumanoidRootPart.Position, workspace.SpawnLocation.Position)
end)
All NPCs have something called a HumanoidRootPart, which is an invisible block in their torso.
Here, we’ve told Roblox that we want that path to compute a path (using ComputeAsync
from the position of the Noob’s HumanoidRootPart to the position of the SpawnLocation.
Remember that now, since there’s an obstacle in the way, the Noob can’t just walk straight to the SpawnLocation. It has to take a detour.

In the illustration above, we can see that in order to avoid the red block, the Noob has to sequentially walk to the various blue blocks.
This means that we have to call :WalkTo
, not straight into the SpawnLocation, but straight to the position of every blue block. In other words, the Noob has to :WalkTo the first blue block’s position, then the second blue block’s position, etc.
Note that this is a list of positions.
Remember the ComputeAsync
? That line of code basically told Roblox to run an algorithm to figure out a list of positions to go from it’s current position to the SpawnLocation, while avoiding the red block.
And now, we need Roblox to actually give us that list of positions (the positions of the blue blocks).
if success and path.Status == Enum.PathStatus.Success then
-- Get the path waypoints
waypoints = path:GetWaypoints()
end
So here, we check to make sure that the path was successful, and we get that list of positions by using path:GetWaypoints()
.
So now, we have a list of positions that the Noob has to walk to, in order to avoid that red block (as well as any other obstacles in the way).
And we just have to cycle through every waypoint and make our Noob move to that position.
local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath()
local success, errorMessage = pcall(function()
path:ComputeAsync(workspace.Noob.HumanoidRootPart.Position, workspace.SpawnLocation.Position)
end)
if success and path.Status == Enum.PathStatus.Success then
waypoints = path:GetWaypoints()
for i, waypoint in next, waypoints do
workspace.Noob.Humanoid:MoveTo(waypoint.Position)
workspace.Noob.Humanoid.MoveToFinished:Wait()
end
end

And there you have it! The Noob has successfully gotten around the red block.
Thank you for reading.
If you like my style of explanation and you learned something, make sure to check out my book, the Beginner’s Guide to Roblox Scripting. The book will teach you Roblox Scripting in a way that is easy to follow.
