RBXStateMachine is a high-performance, full-stack architectural framework for Roblox. It is designed to manage complex gameplay logic, server-authoritative state replication, and CPU budgeting for games that need to scale from a handful of agents to thousands.
It is not just a finite-state-machine library. It is a complete runtime β registry, scheduler, networking, persistence, dev console, and AI primitives β built around four cooperating components: Brain, Body, Pulse, and Nervous System.
π Full documentation lives in the Project Wiki (36 pages, ~500 KB). Start with the 5-Minute Quick Start or the System Architecture deep dive.
The framework enforces a strict separation of concerns through four cooperating components:
| Pillar | Component | Responsibility | Analog |
|---|---|---|---|
| π§ Brain | BaseStateMachine |
Decision making, transitions, AI logic | The thinker |
| π‘οΈ Body | BaseEntity |
Schema-validated data + Instance proxy | The actor |
| β‘ Pulse | Scheduler |
Frame-budgeted time-slicing | The heartbeat |
| πΈοΈ Nervous System | Orchestrator |
Registry, factory, replication, lifecycle | The microkernel |
This separation is what lets you put 500+ NPCs on a server without dropping frames, replicate state to clients without writing a single RemoteEvent, and refactor the AI for one entity without touching the visuals for another.
π See System Architecture for the runtime diagram and bootstrap order.
- β‘ Frame-Budgeted Execution β The Scheduler caps work per frame at a configurable budget (default 5β15 ms) with priority aging so background jobs never starve.
- 𧬠Hierarchical FSMs (HFSM) β Nest sub-machines inside states (e.g. a
Combatstate containingMelee/Ranged). See Hierarchical FSM. - π³ Functional Behavior Trees β Composable
Sequence,Selector,Parallel,Decorator,Action,Conditionnodes viaBehaviorTree. - β»οΈ Resource Tracking β Every FSM/Entity has a
Manage()method that auto-disconnects connections, destroys instances, and runs cleanup callbacks onStop().
- π Transactional Schema β Entities use a stage-and-commit flow (
SetFieldβUpdateEntity()); validation fails atomically with no partial writes. See Replication Pipeline. - π‘ Automatic Replication β Mark a schema field with
Replicate = trueand the framework wires theRemoteEvent, batches deltas, applies rate limits, and sanitizes inbound writes. - πΎ Integrated Persistence β One-line opt-in via
EntityPersistencesaves entity state to DataStores. Includes versioning + migrations (Versioning) and a 7-second write throttle out of the box. - π‘οΈ Server Authority β Inbound
RemoteEventpayloads are sanitized to a configurable depth (default 8) and rate-limited per player.
- π§΅ Parallel Workers β
ActorPooldispatches CPU-heavy jobs to RobloxActorinstances for true parallel execution. - β»οΈ Entity Pooling β Recycle entities across spawns to avoid GC pressure in high-frequency systems (projectiles, particles). See Pooling Strategy.
- π Performance Tuning β Capacity planning, profiling, and budget math: Performance Tuning.
- π₯οΈ Built-in Dev Console β
ServiceManagerships a 9-page in-game UI: logs, entities, FSMs, performance, history, terminal, settings, etc. - π Structured Logger β
Loggerwith a 500-entry circular buffer, log levels, and console streaming. - π§ͺ Spec-style Tests β Full unit + integration suite under
FSM/Tests/(Replication, Pooling, Discovery, Scheduler, Logger, Mocking, etc.). See Testing & Validation.
RBXStateMachine/
βββ FSM/ β The core engine (4 Pillars + auxiliary systems)
β βββ Orchestrator/ β Registry, Factory, ActorPool, ServiceManager, NetworkManager
β βββ Factory/ β BaseEntity, BaseStateMachine, PersistenceManager, Registry
β βββ EntityRegistry.luau β Runtime entity registry
β βββ StateMachineRegistry.luau β Runtime state-machine registry
β βββ Client.client.luau β Client bootstrap script
β βββ Tests/ β Spec-style unit & integration tests
β βββ README.md β Deep technical implementation reference
β
βββ example/ β Working reference implementations
β βββ DoorEntityExample.luau β Schema, replication, instance proxying
β βββ DoorStateMachineExample.luau β Multi-state FSM with validation & failure paths
β βββ GameControllerExample.luau β Bootstrap + entity/FSM creation flow
β
βββ RBXStateMachine.wiki/ β Wiki source (36 pages, comprehensive guide)
The wiki is a separate git repository mounted as a nested directory; it is published at https://github.com/iKryptonic/RBXStateMachine/wiki.
Use Rojo (or your tool of choice) to sync the FSM/ directory into ReplicatedStorage as RBXStateMachine. Place your custom Entity and StateMachine modules as direct children of ReplicatedStorage β the Orchestrator's Factory discovers these folders by name:
ReplicatedStorage/
βββ RBXStateMachine/ β The framework (this repo's FSM/ folder)
βββ Entity/ β Your *Entity.luau modules go here
βββ StateMachine/ β Your *StateMachine.luau modules go here
π Folder layout details: Registration Guide.
-- ServerScriptService/Bootstrap.server.luau
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local FSM = require(ReplicatedStorage:WaitForChild("RBXStateMachine"))
-- Discover Entity/StateMachine modules and wire up remotes, scheduler, logger.
FSM.Orchestrator:RegisterComponents()-- StarterPlayerScripts/Bootstrap.client.luau
local ReplicatedStorage = game:GetService("ReplicatedStorage")
ReplicatedStorage:WaitForChild("Entity")
ReplicatedStorage:WaitForChild("StateMachine")
local FSM = require(ReplicatedStorage:WaitForChild("RBXStateMachine"))
FSM.Orchestrator:RegisterComponents()FSM/Client.client.luau ships as a ready-to-use client bootstrap if you'd rather sync that directly under StarterPlayerScripts.
π Full walkthrough with error handling and verification: Installation Guide.
-- ServerScriptService/Bootstrap.server.luau
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local FSM = require(ReplicatedStorage:WaitForChild("RBXStateMachine"))
FSM.Orchestrator:RegisterComponents()
local part = workspace.SpinningPart -- any Part in workspace
-- 1. Wrap the Instance in a schema-validated Entity.
local entity = FSM.Orchestrator.CreateEntity({
EntityClass = "SpinnerEntity",
EntityId = "Spinner1",
Context = { Instance = part },
})
-- 2. Attach a brain that drives it.
local fsm = FSM.Orchestrator.CreateStateMachine({
StateMachineClass = "SpinnerStateMachine",
StateMachineId = "Spinner1_FSM",
Context = { SpinnerEntity = entity },
})
fsm:Start({ State = "Spinning" })The SpinnerEntity and SpinnerStateMachine modules live under ReplicatedStorage/Entity/ and ReplicatedStorage/StateMachine/ respectively. See example/DoorEntityExample.luau for a complete reference implementation, or the Quick Start for a full walkthrough.
The wiki is structured by audience:
| You want to⦠| Go to |
|---|---|
| Build your first agent in 5 minutes | Quick Start |
| Understand the runtime end-to-end | Architecture |
| Look up a method signature | API Reference |
| Learn how state replicates to clients | Replication Pipeline |
| Build nested AI states | Hierarchical FSM |
| Persist player/entity data | EntityPersistence + Versioning |
| Hit 60 FPS with 500+ NPCs | Performance Tuning + Pooling Strategy |
| Write production-grade code | Best Practices + Production Readiness |
| Diagnose a bug | Debugging + FAQ |
| Migrate from another FSM library | Migration Guide |
| Look up unfamiliar terms | Glossary |
π‘ The Wiki Home has full Beginner / Intermediate / Expert learning paths.
β Use RBXStateMachine if you areβ¦
- Building a multiplayer game with server authority
- Managing 50+ NPCs / agents / entities concurrently
- Need persistent player or world data
- Working in a team where consistent patterns matter
- Building anything where dropped frames are unacceptable
β Consider alternatives if you areβ¦
- Prototyping a 3-day game jam entry
- Building a single-player showcase with simple if/else logic
- Working solo on a small project where the architectural overhead outweighs the benefits
The framework ships with a comprehensive Spec-style test suite under FSM/Tests/:
- Unit tests: Logger, Mocking, Orchestrator, Discovery, Scheduler, Replication Security, Registry, Pool Reuse, Fail-with-nil-history, β¦
- Integration tests: DataStoreEntity, EntityPooling, Factory, ServiceManager, Replication, Iteration Safety, β¦
Run them via your Roblox test runner of choice. See Testing & Validation for setup and authoring conventions.
- π Wiki Home β Full table of contents and learning paths
- π FSM/README.md β Deep technical reference for framework internals
- π¬ FAQ & Troubleshooting β Common questions answered
- π Issues β Bug reports and feature requests welcome