--- name: roblox-npc-ai description: "Use when building, fixing or reviewing Roblox NPCs, bots or enemies: PathfindingService, Humanoid movement, ownership, many-NPC cost." when_to_use: "Also when NPCs get stuck, stop mid-path, walk through walls or doors, take odd routes, jitter or teleport near players, paths fail on long or tall maps, client paths miss objects, or many NPCs lag the server." last_reviewed: 2026-11-29 sources: - https://create.roblox.com/docs/characters/pathfinding - https://create.roblox.com/docs/reference/engine/classes/PathfindingService - https://create.roblox.com/docs/reference/engine/classes/Path - https://create.roblox.com/docs/reference/engine/enums/PathStatus - https://create.roblox.com/docs/reference/engine/classes/PathfindingModifier - https://create.roblox.com/docs/reference/engine/classes/PathfindingLink - https://create.roblox.com/docs/reference/engine/classes/Humanoid - https://create.roblox.com/docs/physics/network-ownership - https://create.roblox.com/docs/workspace/streaming - https://create.roblox.com/docs/projects/server-authority - original --- # Roblox NPC & AI ## When to Load NPC, enemy or bot movement and upkeep: pathfinding, Humanoid walking, ownership, streaming, server cost of many NPCs. Physics ownership in depth: `roblox-physics` §3; profiling: `roblox-performance`; corpses and ragdolls: `roblox-physics` §2. "Field case" = seen in one of the owner's projects; verify on your content. ## Traps and review checklist (symptom -> cause -> fix) - **NPC stops partway on a long leg, `MoveToFinished` returns `false`** -> `Humanoid:MoveTo` times out after 8 s. Keep legs short (waypoints) or call `MoveTo` again on an interval to reset the timer. It also ends on ~1 stud arrival, a new `WalkToPoint`/`WalkToPart`, a `Humanoid:Move()` call, or a script setting the root's `CFrame`, so a teleport or knockback cancels the walk. - **Path fails or hugs walls for a big or small NPC** -> agent defaults are for a standard character: `AgentRadius` 2, `AgentHeight` 5, `AgentCanJump` true, **`AgentCanClimb` false**, `WaypointSpacing` 4. Gaps lower than `AgentHeight` are unwalkable; a goal reachable only by jumping fails with `AgentCanJump = false`. Size per NPC type. - **Path to a far target fails though one exists** -> start-to-finish straight-line distance over 3,000 studs fails, and a search can exhaust its 20,000-node budget well before that in open worlds or mazes. Waypoints with bottom Y outside ±65,536 are ignored. Route through intermediate goals. - **NPC re-paths constantly while fleeing** -> `Path.Blocked` also fires for waypoints **behind** the agent. Recompute only when `blockedWaypointIdx >= nextWaypointIndex`, with a bounded retry; never call the follow function recursively from `Blocked`. `Path.Unblocked` exists for the reverse. - **Path wades through water or lava** -> default search is shortest (it only tries to avoid jumps). `Costs` keys are `Enum.Material` names as strings or `PathfindingModifier.Label`s; `math.huge` = impassable, a high finite cost (100, 1,000) = last resort. - **Region cost or `PassThrough` door has no effect** -> the modifier's part must be `Anchored` with `CanCollide = false` and its `Label` must match a `Costs` key. `PassThrough` makes the path go through the door; the NPC still has to open it. - **Boat, zipline or teleporter never used** -> `PathfindingLink` (`Attachment0` -> `Attachment1`, `Label`) needs a `Costs` entry for its `Label` cheaper than the alternative (`UseBoat = 2` vs `Water = 20`), and your follower must act on the waypoint's `Label` (custom action) instead of walking it. Climb waypoints carry `Label == "Climb"`. - **Client script can't find the NPC or its parts arrive piecemeal** -> under streaming, server NPCs exist on a client only near its focus. Set NPC models `ModelStreamingMode = Atomic` for client visuals; client pathfinding sees only streamed-in geometry, so target parts in `Persistent` models after `Workspace.PersistentLoaded`, and expect streaming to block or unblock paths mid-walk. Server pathfinding sees the whole world. - **Path cost spikes when doors or platforms move** -> moving collidable geometry makes the navigation mesh rebuild. Keep moving blockers `CanCollide = false` where gameplay allows, and capture a MicroProfiler trace before blaming the AI loop. - **Bot reads as "a bot"** -> slow walk speed: 8.5 studs/s was obvious, 11 felt right (field case). Tune reaction delay, aim error and cooldowns instead, with the players' movement numbers. - **Server lags with many NPCs** -> every live NPC is a server-simulated, replicated Humanoid. Think less often, stagger work, and for crowds keep NPCs virtual (§1); measure at the target count. ## Limits | Item | Value | |---|---| | Path request | <= 3,000 studs straight line; 20,000-node budget; waypoints within Y ±65,536 | | 10 active ragdoll NPCs + corpses, one client owner | 0.25 ms physics + 0.6 ms heartbeat per frame (field-measured) | | 40 virtual NPCs drawn as client anchored rigs | ~0.2 ms client script per frame (field-measured) | | Think-rate start point | 0.1 s within ~90 studs, 1 s past ~220 studs; reaction delay 0.15-0.28 s; then profile (field) | ## 1. Many NPCs - **Virtual server, client rigs:** keep position, state and decisions as data on the server (replicated about 20 Hz), and draw each NPC on clients as an anchored rig moved with `workspace:BulkMoveTo` plus default animations. Switch to shared server physics only for bodies and severed pieces; damage stays server-authoritative, gore client-only, collision groups let players walk through piles (field case). - **Sense / think / act:** stagger each bot's raycasts on its own phase; keep decisions in a pure function with no Roblox calls so Lune tests and seeded headless runs can check it; act with player movement numbers. - **Measure progress, not state labels:** log bots that wanted to move 5 of 10 s but moved under 3 studs (with seed and position). Give fixed objectives a no-progress timeout and temporary rejection; moving chase targets need their own replan rule. Claim scarce objectives (cover, items) so a group doesn't converge on one. - **Server Authority:** with `Workspace.AuthorityMode = Server`, ownership is not the authority model; NPC logic stays on the server either way (`roblox-networking`).