State Bags in FiveM: what they are and why they matter
A State Bag is a sticky note you slap on a player, a car, or the whole server, and everyone who needs it sees the same note. Before State Bags, a script that wanted to know "is this player on duty?" had to keep asking the server, over and over. Now you write the answer once, right on the player. Any script reads it instantly, and you only get pinged when it changes. This is how modern FiveM servers share data between resources.
Build it
Create the resource
resources/qu_statebags/
fxmanifest.lua
server.lua
client.lua
fx_version 'cerulean'
game 'gta5'
server_script 'server.lua'
client_script 'client.lua'
Set a player State Bag (server)
-- When a player connects, set their duty status
AddEventHandler('playerJoining', function()
local src = source
local playerState = Player(src).state
-- Set initial values
playerState:set('onduty', false, true) -- true = replicate to all clients
playerState:set('job', 'unemployed', true)
print('Player ' .. src .. ' joined - state initialized')
end)