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 从公开仓库中收录这些内容。

举报或申请下架