Understanding State Bags in FiveM/RedM: A Complete Guide
Introduction
State Bags are a powerful feature in FiveM/RedM that enable efficient data synchronization across multiple players and entities. Introduced in version v2843 as a OneSync feature, State Bags provide a more robust alternative to traditional Decors, offering server-side access and modification capabilities.
Prerequisites
Before diving into State Bags, ensure you have:
OneSync enabled (State Bags won't work without it)
There are three main types of State Bags in FiveM/RedM:
Entity State Bags
Attached to specific entities
Controlled by entity owner and server
Player State Bags
Specific to players
Can be written by the player and server
Global State Bags
Server-wide state
Server-set, client-read
Shared across all resources
Basic Usage
Entity State Bags
-- Server-sidelocal vehicle =GetVehiclePedIsIn(ped,false)local state =Entity(vehicle).state
-- Setting statestate.damage =100-- Getting statelocal currentDamage = state.damage
-- Setting with replication controlEntity(vehicle).state:set('damage',100,true)-- replicate to clients
Player State Bags
-- Server-sidelocal player =Player(source).state
player.health =100-- Client-sideLocalPlayer.state.health =100-- Accessing other player's statelocal playerHealth =Player(id).state.health
-- Replicating from clientLocalPlayer.state:set('health',75,true)-- replicate to server
-- Automatically replicates to all clientsEntity(vehicle).state.fuel =100-- Optional non-replicationEntity(vehicle).state:set('localData','value',false)
Client-Side
-- Local only by defaultEntity(vehicle).state.customData ='value'-- Explicit replicationEntity(vehicle).state:set('customData','value',true)
Player State Replication
-- Server controls replicationPlayer(source).state.isAlive =true-- replicates-- Client needs explicit replicationLocalPlayer.state:set('isReady',true,true)-- replicates to server
State Bag Change Handlers
State Bag change handlers allow you to monitor and react to state changes in real-time.
-- Explosion handler based on stateAddStateBagChangeHandler('exploded',nil,function(bagName, key, value, _unused, replicated)-- Only process when value is trueifnot value thenreturnend-- Get entity from bag namelocal entity =GetEntityFromStateBagName(bagName)if entity ==0thenreturnend-- Get coordinates and create explosionlocal coords =GetEntityCoords(entity)AddExplosion(coords,0xFFFFFFFF,1.0,true,false,1.0)end)
Best Practices and Limitations
State Access Optimization
-- Inefficientlocal x =Entity(vehicle).state.data.x
local y =Entity(vehicle).state.data.y
-- Better Approachlocal state =Entity(vehicle).state
local x = state.data.x
local y = state.data.y
Shallow State Limitations
-- Won't work (nested modification)Entity(vehicle).state.data.subset ='value'-- Correct approachEntity(vehicle).state['data:subset']='value'
Data Size Considerations
-- For large data, use latent events instead-- Bad:Entity(vehicle).state.hugeData = veryLargeTable
-- Better:TriggerLatentClientEvent('syncLargeData', player,1000, veryLargeTable)
-- Server-side player statusRegisterNetEvent('playerLoaded',function()Player(source).state.hunger =100Player(source).state.thirst =100end)-- Client-side status updateCreateThread(function()whiletruedoWait(60000)-- Every minute LocalPlayer.state:set('hunger',(LocalPlayer.state.hunger or100)-1,true) LocalPlayer.state:set('thirst',(LocalPlayer.state.thirst or100)-1.5,true)endend)
-- Always check if state existslocal damage =Entity(vehicle).state.damage
if damage ==nilthen damage =0end
Replication Issues
-- Ensure proper replication flagsLocalPlayer.state:set('status','ready',true)-- Must set true for client->server
Conclusion
State Bags provide a powerful and efficient way to synchronize data across your FiveM/RedM server. By following these best practices and understanding the limitations, you can create robust and performant systems for your server.
Remember key points:
Use appropriate state bag types for your needs
Be mindful of data size and replication
Implement proper error handling
Consider performance implications in high-traffic scenarios