$npx -y skills add TabooHarmony/roblox-brain --skill roblox-npc-aiUse when creating Roblox NPCs or enemies with pathfinding, state machines, line-of-sight or FOV detection, spawns, or AI update loops.
| 1 | # Roblox NPC & AI |
| 2 | |
| 3 | ## When to Load |
| 4 | |
| 5 | Load for NPCs/enemies: pathfinding, state machines, LOS/FOV detection, spawns, AI loops, or physics ownership. |
| 6 | |
| 7 | ## Quick Reference |
| 8 | |
| 9 | ### PathfindingService |
| 10 | |
| 11 | ```luau |
| 12 | local path = PathfindingService:CreatePath({ |
| 13 | AgentRadius = 2, AgentHeight = 5, AgentCanJump = true, |
| 14 | Costs = { Water = 20, DangerZone = math.huge }, |
| 15 | }) |
| 16 | path:ComputeAsync(npcPos, targetPos) |
| 17 | if path.Status ~= Enum.PathStatus.Success then return end |
| 18 | for _, wp in path:GetWaypoints() do |
| 19 | if wp.Action == Enum.PathWaypointAction.Jump then humanoid.Jump = true end |
| 20 | humanoid:MoveTo(wp.Position) |
| 21 | if not humanoid.MoveToFinished:Wait() then return end |
| 22 | end |
| 23 | ``` |
| 24 | |
| 25 | - Recompute on blocked with a bounded retry or cancellation policy. |
| 26 | - Test `Workspace.PathfindingUseImprovedSearch` on representative maps before rollout. |
| 27 | - Avoid long requests and repeated recomputation. Moving collidable geometry can trigger navigation-mesh work. |
| 28 | - Region modifiers: anchored part + `PathfindingModifier` Label → Costs |
| 29 | - `PassThrough = true` for doors; `PathfindingLink` for disconnected navmesh |
| 30 | - `math.huge` cost = non-traversable |
| 31 | |
| 32 | ### State Machine |
| 33 | |
| 34 | `idle → patrol → chase → attack → flee → dead` |
| 35 | - idle/patrol → chase (player detected) → attack (in range) → idle (lost) |
| 36 | - any → flee (health low) → idle (safe); any → dead (health ≤ 0) |
| 37 | - Transitions own movement changes, target cleanup, and cancellation |
| 38 | |
| 39 | ### Detection (distance → FOV → LOS) |
| 40 | |
| 41 | 1. **Distance** `(a-b).Magnitude` — cheapest, always first |
| 42 | 2. **FOV** `forward:Dot(toTarget)` cosine — use a configured cone for the game, not a universal angle |
| 43 | 3. **LOS** `workspace:Raycast` — expensive, last |
| 44 | - If the design includes hearing or proximity detection, make it a separate configured signal rather than a universal FOV bypass. |
| 45 | |
| 46 | ### Network Ownership |
| 47 | - 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. |
| 48 | |
| 49 | ### Update Loop |
| 50 | |
| 51 | - 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. |
| 52 | |
| 53 | For spawners, lifecycle cleanup, timeout handling, and performance budgets, load `references/full.md`. |