Create non-player characters that patrol, chase, and navigate around obstacles using PathfindingService.
A game with only feels empty. NPCs — non- characters that walk around, patrol routes, or chase the — bring the world to life. PathfindingService is the engine that lets them navigate around walls and obstacles instead of walking in a straight line into a wall.
An NPC is just a character model with a Humanoid — no attached. You can move it with :MoveTo():
local npc = workspace.NPC -- a model with a Humanoid inside
local humanoid = npc:WaitForChild("Humanoid")
humanoid:MoveTo(Vector3.new(50, 0, -20))
:MoveTo() tells the Humanoid to walk to a world position. It handles turning, walking speed, and stopping when it arrives. The NPC will walk in a straight line — no obstacle avoidance yet.
The Humanoid method that makes a character walk toward a world position is .
A straight line walks through walls. PathfindingService computes a route that goes around them:
local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = true,
})
path:ComputeAsync(startPosition, endPosition)
AgentRadius / AgentHeight — the size of the character (must fit through gaps).AgentCanJump — whether the NPC can jump over small obstacles.:ComputeAsync() — calculates the path. Status tells you if it succeeded.PathfindingService works on the server. If you need client-side prediction (seeing the path before the server moves), compute it in a LocalScript — but the actual movement must happen server-side.
Once computed, extract the waypoints and walk through them:
if path.Status == Enum.PathStatus.Success then
local waypoints = path:GetWaypoints()
for _, waypoint in ipairs(waypoints) do
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
if waypoint.Action == Enum.PathWaypointAction.Jump then
humanoid.Jump = true
end
end
end
Each waypoint has a Position and an Action (walk or jump). Walk to each one in order, wait for arrival, and handle jumps. The NPC smoothly navigates around walls, stairs, and gaps.
After computing a path, write a for loop that walks the NPC through each waypoint and waits for it to arrive.
local waypoints = path:GetWaypoints()\n-- walk through each waypointMost NPCs repeat a route — patrol, chase, return. A simple :
local points = {
Vector3.new(10, 0, 10),
Vector3.new(50, 0, 10),
Vector3.new(50, 0, -20),
Vector3.new(10, 0, -20),
}
while true do
for _, pos in ipairs(points) do
humanoid:MoveTo(pos)
humanoid.MoveToFinished:Wait()
end
task.wait(1) -- pause at the end before repeating
end
The NPC walks the route, pauses, and repeats. For a chase, replace the target with the 's position and recompute the path each time.
Why do you wait for MoveToFinished before calling MoveTo again?
MoveToFinished before the next walk.ComputeAsync doesn't block — if you read waypoints immediately, they're empty. Call :ComputeAsync() first, then read the result.GetWaypoints() returns a single point. Check Status == Enum.PathStatus.Success.Next: sticky notes for your objects — attributes.