Roblox NPC & AI
When to Load
Load for NPCs/enemies: pathfinding, state machines, LOS/FOV detection, spawns, AI loops, or physics ownership.
Quick Reference
PathfindingService
local path = PathfindingService:CreatePath({
AgentRadius = 2, AgentHeight = 5, AgentCanJump = true,
Costs = { Water = 20, DangerZone = math.huge },
})
path:ComputeAsync(npcPos, targetPos)
if path.Status ~= Enum.PathStatus.Success then return end
for _, wp in path:GetWaypoints() do
if wp.Action == Enum.PathWaypointAction.Jump then humanoid.Jump = true end
humanoid:MoveTo(wp.Position)
if not humanoid.MoveToFinished:Wait() then return end
end
- Recompute on blocked with a bounded retry or cancellation policy.
- In Studio, test
Workspace.PathfindingUseImprovedSearch on representative maps before rollout; it is not scriptable.
- Avoid long requests and repeated recomputation. Moving collidable geometry can trigger navigation-mesh work.
- Region modifiers: anchored part +
PathfindingModifier Label → Costs
PassThrough = true for doors; PathfindingLink for disconnected navmesh
math.huge cost = non-traversable
State Machine
idle → patrol → chase → attack → flee → dead
- idle/patrol → chase (player detected) → attack (in range) → idle (lost)
- any → flee (health low) → idle (safe); any → dead (health ≤ 0)
- Transitions own movement changes, target cleanup, and cancellation
Detection (distance → FOV → LOS)
- Distance
(a-b).Magnitude: cheapest, always first
- FOV
forward:Dot(toTarget) cosine; use a configured cone for your game, not a universal angle
- LOS
workspace:Raycast: expensive, last
- If the design includes hearing or proximity detection, make it a separate configured signal rather than a universal FOV bypass.
Network Ownership
- Classic projects may use
SetNetworkOwner(nil) for physics-sensitive NPCs, but it is not complete security. In Server Authority projects, configure the authority model instead of treating network ownership as the security boundary.
Update Loop
- Throttle and stagger AI based on NPC count, path cost, and profiler evidence, not a universal tick rate. Keep NPC decisions and movement server-side; client code may handle presentation.
For spawners, lifecycle cleanup, timeout handling, and performance budgets, load references/full.md.
1---2name: roblox-npc-ai3description: "Use when creating Roblox NPCs or enemies with pathfinding, state machines, line-of-sight or FOV detection, spawns, or AI update loops."4last_reviewed: 2026-08-315sources:6 - https://raw.githubusercontent.com/Roblox/creator-docs/main/content/en-us/characters/pathfinding.md7 - https://create.roblox.com/docs/projects/server-authority8---910# Roblox NPC & AI1112## When to Load1314Load for NPCs/enemies: pathfinding, state machines, LOS/FOV detection, spawns, AI loops, or physics ownership.1516## Quick Reference1718### PathfindingService1920```luau21local path = PathfindingService:CreatePath({22 AgentRadius = 2, AgentHeight = 5, AgentCanJump = true,23 Costs = { Water = 20, DangerZone = math.huge },24})25path:ComputeAsync(npcPos, targetPos)26if path.Status ~= Enum.PathStatus.Success then return end27for _, wp in path:GetWaypoints() do28 if wp.Action == Enum.PathWaypointAction.Jump then humanoid.Jump = true end29 humanoid:MoveTo(wp.Position)30 if not humanoid.MoveToFinished:Wait() then return end31end32```3334- Recompute on blocked with a bounded retry or cancellation policy.35- In Studio, test `Workspace.PathfindingUseImprovedSearch` on representative maps before rollout; it is not scriptable.36- Avoid long requests and repeated recomputation. Moving collidable geometry can trigger navigation-mesh work.37- Region modifiers: anchored part + `PathfindingModifier` Label → Costs38- `PassThrough = true` for doors; `PathfindingLink` for disconnected navmesh39- `math.huge` cost = non-traversable4041### State Machine4243`idle → patrol → chase → attack → flee → dead`44- idle/patrol → chase (player detected) → attack (in range) → idle (lost)45- any → flee (health low) → idle (safe); any → dead (health ≤ 0)46- Transitions own movement changes, target cleanup, and cancellation4748### Detection (distance → FOV → LOS)49501. **Distance** `(a-b).Magnitude`: cheapest, always first512. **FOV** `forward:Dot(toTarget)` cosine; use a configured cone for your game, not a universal angle523. **LOS** `workspace:Raycast`: expensive, last53- If the design includes hearing or proximity detection, make it a separate configured signal rather than a universal FOV bypass.5455### Network Ownership56- Classic projects may use `SetNetworkOwner(nil)` for physics-sensitive NPCs, but it is not complete security. In Server Authority projects, configure the authority model instead of treating network ownership as the security boundary.5758### Update Loop5960- Throttle and stagger AI based on NPC count, path cost, and profiler evidence, not a universal tick rate. Keep NPC decisions and movement server-side; client code may handle presentation.6162For spawners, lifecycle cleanup, timeout handling, and performance budgets, load `references/full.md`.