Introduction
The RSG Framework uses FiveMโs StateBags system to efficiently sync player metadata between server and client in real-time. StateBags provide a performant alternative to constantly triggering events, allowing instant access to player data on both sides. Version: 2.3.6+StateBags automatically sync between server and client without requiring events. Changes are instant and bidirectional!
Core Features
๐ Real-Time Synchronization
- Instant Updates: Changes sync immediately without event delays
- Bidirectional: Server โ Client synchronization
- Automatic: No manual event triggering needed
- Efficient: Lower network overhead than events
๐ Synced Metadata
The framework automatically syncs these metadata values:โก Performance Benefits
- No Event Spam: Eliminates constant event triggering
- Direct Access: Read values directly without callbacks
- Reduced Latency: Instant synchronization
- Cleaner Code: No callback hell or event chains
How It Works
Architecture Overview
Initialization on Player Login
When a player logs in, their metadata is loaded and synced to StateBags:Persistence on Player Save
When player data is saved, StateBag values are persisted back to metadata:Reading StateBags
Server-Side Access
Client-Side Access
Watching for Changes
Client-Side State Change Handlers
Server-Side State Change Handlers
Common Use Cases
Example 1: Custom HUD System
Example 2: Needs System with Debuffs
Example 3: Hunger/Thirst Decrease System
Example 4: Consumable Items Using StateBags
Example 5: Doctor Healing Using Health StateBag
Integration with Player Functions
SetMetaData Integration
When you usePlayer.Functions.SetMetaData(), it automatically updates StateBags:
Direct StateBag Updates
You can also update StateBags directly (faster, but bypasses metadata tracking):When to Use StateBags vs Events
Use StateBags For:
โ Frequently updated data- Hunger, thirst, health changes
- Real-time stat updates
- Player status flags
- HUD displays
- Status effects
- Proximity checks
- Number values
- Boolean flags
- String identifiers
Use Events For:
โ Complex data updates- Nested table structures
- Large data payloads
- Multiple related changes
- Money transactions
- Inventory changes
- Permission checks
- Achievement unlocks
- Quest completions
- Single notifications
Best Practices
Always provide fallback values: Use
or operator when reading StateBags in case they havenโt been initialized yetPerformance Guidelines
- Read frequently, write sparingly: StateBags are optimized for reading
- Batch updates when possible: Multiple state changes in quick succession can be batched
- Use state change handlers: More efficient than polling in loops
- Avoid redundant writes: Only update if value actually changed
Code Examples
Good Practice:Troubleshooting
StateBag values are nil or undefined
StateBag values are nil or undefined
- Ensure player is logged in (
isLoggedInshould be true) - Check that InitializeStateBags was called on server
- Provide fallback values:
LocalPlayer.state.hunger or 100
State changes not syncing to client
State changes not syncing to client
- Verify youโre updating the correct playerโs state
- Check that the state key is one of the synced keys
- Ensure player is still connected
State change handler firing multiple times
State change handler firing multiple times
- Always check if bagName matches the intended player
- Use debouncing if needed for frequent updates
- Remove handlers when no longer needed
Performance issues with state changes
Performance issues with state changes
- Donโt update states too frequently (< 100ms between updates)
- Batch multiple state changes if possible
- Use state change handlers instead of polling loops
Technical Details
StateBag Format
State bags are accessed via thePlayer() native:
Synced vs Non-Synced States
Only these keys are automatically persisted to database:hunger,thirst,cleanliness,stress,health
Network Replication
StateBags use FiveMโs native replication system:- Changes replicate to all clients in range
- Routing bucket aware
- Automatic cleanup on player disconnect
Next Steps
- Player Data Reference - Complete player data structure
- Configuration Guide - Configure default metadata values
- Server Functions - All player functions
Need help? Join the RSG Framework Discord!