Skip to main content

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 use Player.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
โœ… Data that needs instant client access
  • HUD displays
  • Status effects
  • Proximity checks
โœ… Simple value synchronization
  • Number values
  • Boolean flags
  • String identifiers

Use Events For:

โŒ Complex data updates
  • Nested table structures
  • Large data payloads
  • Multiple related changes
โŒ Actions that need validation
  • Money transactions
  • Inventory changes
  • Permission checks
โŒ One-time notifications
  • Achievement unlocks
  • Quest completions
  • Single notifications

Best Practices

Use StateBags for frequently read data: If you check a value in a loop, use StateBags instead of triggering events every frame
Donโ€™t overwrite entire tables: StateBags work best with primitive values (numbers, strings, booleans). For complex tables, use metadata or events
Always provide fallback values: Use or operator when reading StateBags in case they havenโ€™t been initialized yet

Performance Guidelines

  1. Read frequently, write sparingly: StateBags are optimized for reading
  2. Batch updates when possible: Multiple state changes in quick succession can be batched
  3. Use state change handlers: More efficient than polling in loops
  4. Avoid redundant writes: Only update if value actually changed

Code Examples

Good Practice:
Bad Practice:

Troubleshooting

  • Ensure player is logged in (isLoggedIn should be true)
  • Check that InitializeStateBags was called on server
  • Provide fallback values: LocalPlayer.state.hunger or 100
  • 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
  • Always check if bagName matches the intended player
  • Use debouncing if needed for frequent updates
  • Remove handlers when no longer needed
  • 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 the Player() native:

Synced vs Non-Synced States

Only these keys are automatically persisted to database:
  • hunger, thirst, cleanliness, stress, health
Other custom states can be created but wonโ€™t persist:

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


Need help? Join the RSG Framework Discord!