roblox-npcs
Official sources (always check these for the latest):
This skill covers navigation mesh pathfinding and the AI patterns that use it. It does not cover custom A* implementations unless absolutely necessary — PathfindingService is the official, optimized solution.
When to use this skill
Activate when:
- Building zombies, guards, pets, companions, or any AI that walks/follows/patrols.
- Tuning agent size, jump/climb ability, or preferred terrain.
- Handling dynamic obstacles and blocked paths.
- Using
PathfindingModifier regions/links for doors, traps, ladders, boats.
- Scaling pathfinding for many agents.
Cross-reference:
PathfindingService basics
Create a path:
local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = true,
AgentCanClimb = false,
WaypointSpacing = 4,
Costs = {
Water = 20,
DangerZone = math.huge,
}
})
path.CalculationSecondsTimeout = 1
Compute and follow:
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")
local success, err = pcall(function()
path:ComputeAsync(rootPart.Position, endPos)
end)
if success and path.Status == Enum.PathStatus.Success then
local waypoints = path:GetWaypoints()
-- follow waypoints with Humanoid:Move()
end
Agent parameters
| Parameter |
Default |
Purpose |
AgentRadius |
2 studs |
Minimum clearance from obstacles |
AgentHeight |
5 studs |
Vertical clearance |
AgentCanJump |
true |
Allows jump waypoints |
AgentCanClimb |
false |
Allows climbing truss parts |
WaypointSpacing |
4 studs |
Distance between intermediate waypoints |
Costs |
nil |
Material/region/link traversal cost |
Path.CalculationSecondsTimeout limits how long the solver may run per ComputeAsync call. Set it after CreatePath and before computing.
PathWaypoint actions
Each waypoint has a Position and an Action:
Enum.PathWaypointAction.Walk — normal movement.
Enum.PathWaypointAction.Jump — trigger jump.
- Custom labels like
"Climb" or "UseBoat" from PathfindingModifiers/Links.
Pathfinding modifiers
PathfindingModifier instances on anchored, non-colliding parts let you influence path cost:
Label — key used in Costs table.
PassThrough — if true, the volume is ignored by the navmesh and treated as traversable empty space (e.g., zombies "hearing" through doors).
Example:
local path = PathfindingService:CreatePath({
Costs = {
Water = 20,
DangerZone = math.huge,
UseBoat = 2,
}
})
Pathfinding links
PathfindingLink connects two Attachments with a custom label and cost, allowing paths across normally untraversable gaps.
Use for:
- Boats across water
- Teleporters
- Ladders
- One-way jumps
Your movement code checks the waypoint label and runs the custom traversal logic.
Movement patterns
Follow
Continuously recompute a path to a moving target. Throttle recomputation (e.g., every 0.5–1 s) and only recompute if the target moved far enough.
Patrol
Cycle through a list of fixed points. Recompute when blocked.
Chase
Like follow, but validate line-of-sight and distance server-side. Don't trust client-reported positions for authoritative AI.
State machine
Common NPC states: Idle, Patrol, Chase, Attack, Return. Each state handles its own path computation and Humanoid control.
Streaming compatibility
- Server-side scripts have full world state and can compute paths to any part.
- Client-side scripts may fail if the destination has streamed out. Use
workspace.PersistentLoaded and persistent models for client path destinations.
- Recompute paths when dynamic/streamed obstacles block the way.
Limitations
- Direct line-of-sight distance ≤ 3,000 studs.
- Computation node budget ≈ 20,000 nodes.
- Waypoint Y coordinate must be between -65,536 and +65,536 studs.
- Incompatible parameters (e.g.,
AgentCanJump = false to a jump-only destination) will fail.
Performance at scale
- Recompute paths on a staggered schedule, not every frame.
- Share target positions across similar NPCs when possible.
- Use
WaypointSpacing = math.huge to reduce intermediate waypoints for long straight runs.
- Consider simplifying agent geometry or using fewer active agents.
- For very large worlds, split into regions or use local patrol paths.
Common mistakes this skill prevents
- Computing paths every frame.
- Ignoring blocked-path events and letting NPCs walk into walls.
- Trusting client position for authoritative AI.
- Forgetting
pcall around ComputeAsync.
- Using material names incorrectly in
Costs (must match Enum.Material names as strings).
Scripts
scripts/NPCPathFollower.lua — Humanoid-based path follower with blocked-path recompute, custom-label support, and connection cleanup.
scripts/PatrolBehavior.lua — state-driven patrol/chase behavior with spatial detection and throttled recomputation.
scripts/PathfindingUtility.lua — helpers for throttled recomputation and waypoint formatting.
Best practices
- Set
Path.CalculationSecondsTimeout after CreatePath to cap solver time.
- Always set an explicit
Humanoid:MoveTo timeout and cancel it when the waypoint is reached or the follower is stopped.
- Detect targets with spatial queries such as
workspace:GetPartBoundsInRadius instead of scanning every player each frame.
- Stop path followers and clean up
Heartbeat connections when the Humanoid dies or the NPC is destroyed.
- For respawning NPCs, create a new behavior instance for the new character model and
Destroy the old one.
- Use
PathfindingLink labels to trigger custom traversal logic (boats, teleporters, ladders). The follower invokes a registered handler; if none exists, the waypoint falls back to normal movement.
- To enable climbing, set
AgentCanClimb = true and provide TrussPart surfaces. Climb waypoints have the Label "Climb".
PathfindingModifier parts must be Anchored = true and CanCollide = false.
How to proceed
- Define the agent's size and movement abilities.
- Build the world with modifiers/links for special regions.
- Implement a path-follower that handles waypoints, jumps, and blocked events.
- Layer a state machine for complex behaviors.
- Run on the server for authoritative AI; use client only for visual prediction.
- Profile with MicroProfiler and stagger recomputation for many agents.
Reference index
- modifiers-links-and-streaming.md
- npc-behavior-patterns.md
- pathfinding-service-details.md
- performance-and-scaling.md
1---2name: roblox-npcs3description: Roblox pathfinding and NPC AI — PathfindingService, agent parameters, waypoint actions, blocked-path handling, PathfindingModifier/Link, material and region costs, and streaming compatibility. Covers NPC design patterns: state machines, behavior trees, follow/patrol/chase, humanoid movement, obstacle avoidance, and performance at scale. Use for NPCs, enemy AI, companions, patrols, or any agent that navigates the 3D world.4---56# roblox-npcs78**Official sources (always check these for the latest):**9- https://create.roblox.com/docs/en-us/characters/pathfinding10- https://create.roblox.com/docs/en-us/workspace/streaming11- Engine classes: `PathfindingService`, `Path`, `PathWaypoint`, `PathfindingModifier`, `PathfindingLink`, `Humanoid`1213This skill covers navigation mesh pathfinding and the AI patterns that use it. It does not cover custom A* implementations unless absolutely necessary — PathfindingService is the official, optimized solution.1415## When to use this skill1617Activate when:18- Building zombies, guards, pets, companions, or any AI that walks/follows/patrols.19- Tuning agent size, jump/climb ability, or preferred terrain.20- Handling dynamic obstacles and blocked paths.21- Using `PathfindingModifier` regions/links for doors, traps, ladders, boats.22- Scaling pathfinding for many agents.2324Cross-reference:25- [roblox-core/SKILL.md](../roblox-core/SKILL.md) for services and Humanoid basics.26- [roblox-networking/SKILL.md](../roblox-networking/SKILL.md) for server-authoritative AI.27- [roblox-physics/SKILL.md](../roblox-physics/SKILL.md) for custom non-humanoid rigs and mover constraints.28- [roblox-testing/SKILL.md](../roblox-testing/SKILL.md) for profiling AI cost.2930## PathfindingService basics3132Create a path:3334```lua35local PathfindingService = game:GetService("PathfindingService")3637local path = PathfindingService:CreatePath({38 AgentRadius = 2,39 AgentHeight = 5,40 AgentCanJump = true,41 AgentCanClimb = false,42 WaypointSpacing = 4,43 Costs = {44 Water = 20,45 DangerZone = math.huge,46 }47})48path.CalculationSecondsTimeout = 149```5051Compute and follow:5253```lua54local humanoid = character:WaitForChild("Humanoid")55local rootPart = character:WaitForChild("HumanoidRootPart")5657local success, err = pcall(function()58 path:ComputeAsync(rootPart.Position, endPos)59end)6061if success and path.Status == Enum.PathStatus.Success then62 local waypoints = path:GetWaypoints()63 -- follow waypoints with Humanoid:Move()64end65```6667## Agent parameters6869| Parameter | Default | Purpose |70| --- | --- | --- |71| `AgentRadius` | 2 studs | Minimum clearance from obstacles |72| `AgentHeight` | 5 studs | Vertical clearance |73| `AgentCanJump` | true | Allows jump waypoints |74| `AgentCanClimb` | false | Allows climbing truss parts |75| `WaypointSpacing` | 4 studs | Distance between intermediate waypoints |76| `Costs` | nil | Material/region/link traversal cost |7778`Path.CalculationSecondsTimeout` limits how long the solver may run per `ComputeAsync` call. Set it after `CreatePath` and before computing.7980## PathWaypoint actions8182Each waypoint has a `Position` and an `Action`:83- `Enum.PathWaypointAction.Walk` — normal movement.84- `Enum.PathWaypointAction.Jump` — trigger jump.85- Custom labels like `"Climb"` or `"UseBoat"` from PathfindingModifiers/Links.8687## Pathfinding modifiers8889`PathfindingModifier` instances on anchored, non-colliding parts let you influence path cost:90- `Label` — key used in `Costs` table.91- `PassThrough` — if `true`, the volume is ignored by the navmesh and treated as traversable empty space (e.g., zombies "hearing" through doors).9293Example:9495```lua96local path = PathfindingService:CreatePath({97 Costs = {98 Water = 20,99 DangerZone = math.huge,100 UseBoat = 2,101 }102})103```104105## Pathfinding links106107`PathfindingLink` connects two `Attachment`s with a custom label and cost, allowing paths across normally untraversable gaps.108109Use for:110- Boats across water111- Teleporters112- Ladders113- One-way jumps114115Your movement code checks the waypoint label and runs the custom traversal logic.116117## Movement patterns118119### Follow120121Continuously recompute a path to a moving target. Throttle recomputation (e.g., every 0.5–1 s) and only recompute if the target moved far enough.122123### Patrol124125Cycle through a list of fixed points. Recompute when blocked.126127### Chase128129Like follow, but validate line-of-sight and distance server-side. Don't trust client-reported positions for authoritative AI.130131### State machine132133Common NPC states: Idle, Patrol, Chase, Attack, Return. Each state handles its own path computation and Humanoid control.134135## Streaming compatibility136137- Server-side scripts have full world state and can compute paths to any part.138- Client-side scripts may fail if the destination has streamed out. Use `workspace.PersistentLoaded` and persistent models for client path destinations.139- Recompute paths when dynamic/streamed obstacles block the way.140141## Limitations142143- Direct line-of-sight distance ≤ 3,000 studs.144- Computation node budget ≈ 20,000 nodes.145- Waypoint Y coordinate must be between -65,536 and +65,536 studs.146- Incompatible parameters (e.g., `AgentCanJump = false` to a jump-only destination) will fail.147148## Performance at scale149150- Recompute paths on a staggered schedule, not every frame.151- Share target positions across similar NPCs when possible.152- Use `WaypointSpacing = math.huge` to reduce intermediate waypoints for long straight runs.153- Consider simplifying agent geometry or using fewer active agents.154- For very large worlds, split into regions or use local patrol paths.155156## Common mistakes this skill prevents157158- Computing paths every frame.159- Ignoring blocked-path events and letting NPCs walk into walls.160- Trusting client position for authoritative AI.161- Forgetting `pcall` around `ComputeAsync`.162- Using material names incorrectly in `Costs` (must match `Enum.Material` names as strings).163164## Scripts165166- `scripts/NPCPathFollower.lua` — Humanoid-based path follower with blocked-path recompute, custom-label support, and connection cleanup.167- `scripts/PatrolBehavior.lua` — state-driven patrol/chase behavior with spatial detection and throttled recomputation.168- `scripts/PathfindingUtility.lua` — helpers for throttled recomputation and waypoint formatting.169170## Best practices171172- Set `Path.CalculationSecondsTimeout` after `CreatePath` to cap solver time.173- Always set an explicit `Humanoid:MoveTo` timeout and cancel it when the waypoint is reached or the follower is stopped.174- Detect targets with spatial queries such as `workspace:GetPartBoundsInRadius` instead of scanning every player each frame.175- Stop path followers and clean up `Heartbeat` connections when the `Humanoid` dies or the NPC is destroyed.176- For respawning NPCs, create a new behavior instance for the new character model and `Destroy` the old one.177- Use `PathfindingLink` labels to trigger custom traversal logic (boats, teleporters, ladders). The follower invokes a registered handler; if none exists, the waypoint falls back to normal movement.178- To enable climbing, set `AgentCanClimb = true` and provide `TrussPart` surfaces. Climb waypoints have the `Label` `"Climb"`.179- `PathfindingModifier` parts must be `Anchored = true` and `CanCollide = false`.180181## How to proceed1821831. Define the agent's size and movement abilities.1842. Build the world with modifiers/links for special regions.1853. Implement a path-follower that handles waypoints, jumps, and blocked events.1864. Layer a state machine for complex behaviors.1875. Run on the server for authoritative AI; use client only for visual prediction.1886. Profile with MicroProfiler and stagger recomputation for many agents.189190<!-- catalog:references:start -->191## Reference index192193- [modifiers-links-and-streaming.md](references/modifiers-links-and-streaming.md)194- [npc-behavior-patterns.md](references/npc-behavior-patterns.md)195- [pathfinding-service-details.md](references/pathfinding-service-details.md)196- [performance-and-scaling.md](references/performance-and-scaling.md)197<!-- catalog:references:end -->