NPC Systems and AI in Roblox
Create non-player characters with dialogue, pathfinding, and behavior trees for immersive game worlds.
Introduction
NPCs bring game worlds to life—shopkeepers, quest givers, enemies, and companions. Roblox NPCs combine models in Workspace with scripts controlling movement (PathfindingService), dialogue (GUI or chat), and behavior state machines. This hub covers practical NPC patterns for common game genres.
Dialogue Systems
Store dialogue as tables: { npc = 'Merchant', lines = { 'Welcome!', 'Buy something?' }, choices = { 'Shop', 'Leave' } }. Display in a ScreenGui dialogue box. Advance on button click. Branch based on player choices. Keep dialogue data in ModuleScripts for easy editing.
NPC Movement with PathfindingService
PathfindingService:CreatePath() computes routes around obstacles. Call :ComputeAsync(start, goal), then :GetWaypoints() for the path. Move NPC Humanoid to each waypoint. Handle blocked paths with recalculation. Set AgentRadius and AgentHeight to match your NPC model.
Use Cases
- Quest NPCs
- Enemy AI patrols
- Companion followers
Best Practices
- Run pathfinding server-side for authoritative NPC positions
- Limit concurrent path computations for performance
- Use simple state machines for NPC behavior
Troubleshooting
Script runs but nothing happens in-game
Verify the script type matches its location. Server Scripts belong in ServerScriptService; LocalScripts in StarterPlayerScripts or StarterGui. Check the Output window in Studio for error messages.
Changes don't appear after editing
Stop and restart Play mode in Studio. Some scripts cache values on first run. For published games, republish after testing locally.