Roblox Npc Ai

TabooHarmony/roblox-brain/skills/gameplay/roblox-npc-ai

作者 TabooHarmony38826be57ee37bcf023e9c2b85681bea3909281c無授權條款收錄於 2026年10月9日更新於 2026年10月9日

Use when creating Roblox NPCs or enemies with pathfinding, state machines, line-of-sight or FOV detection, spawns, or AI update loops.

AI 產生的概覽

指導 Roblox 開發者打造具備尋路、狀態機、偵測與更新迴圈的 NPC 與敵人 AI。

功能
此技能為實作 Roblox NPC 與敵人行為提供參考指引,涵蓋 PathfindingService 的用法、從 idle 到 patrol、chase、attack、flee、dead 的狀態機流程,以及結合距離、視野與射線偵測的分層偵測方式。它也討論網路所有權與伺服器權威的考量,以及如何對 AI 更新迴圈進行節流。它產出的是實作指引與程式碼片段,而非檔案或指令碼。
適用情境
在建立需要尋路、狀態機、視線或視野偵測、生成器或 AI 更新迴圈的 Roblox NPC 或敵人時使用。在思考 NPC 的物理所有權或伺服器權威問題時也適用。
執行需求
不含指令碼,僅為說明性指引。它假定執行環境為 Roblox Studio 或 Roblox 伺服器,使用 Luau 及其引用的 Roblox API,並指向隨附的參考檔案以了解生成器、生命週期清理、逾時處理與效能預算。

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

luau
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 endfor _, 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 endend
  • 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)

  1. Distance (a-b).Magnitude: cheapest, always first
  2. FOV forward:Dot(toTarget) cosine; use a configured cone for your game, not a universal angle
  3. 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.

來源與署名

來源:TabooHarmony/roblox-brain位於skills/gameplay/roblox-npc-ai提交38826be

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架