LUA state management
Keep changing data predictable and make every screen show the current source of truth.
Chapter goal: Keep changing data predictable and make every screen show the current source of truth.
Simple explanation
State is the current memory of an app or screen. A source of truth is the one place that owns the correct current value.
In LUA, this chapter is about deciding who owns changing data and who is allowed to update it. Start with the idea above. Then connect each symbol to a value or action in the example.
Do not try to remember every symbol. First ask what data the program has, what it does with that data, and what result it creates. Technical words become easier when you connect them to those three questions.
Why this topic is important
Unclear state causes stale screens, duplicated values, and changes that are difficult to trace. In LUA, the syntax may look different from other languages, but the thinking skill transfers: name the data, choose the right operation, and make the next step obvious.
When to use it
Manage state for counters, forms, authentication, carts, loading, errors, filters, and cached server data.
Example code
local playerState = {coins = 10}
local function addCoins(amount)
playerState.coins = playerState.coins + amount
end
addCoins(5)
print(playerState.coins)
Line-by-line explanation
local playerState = {coins = 10}— This stores or updatesplayerState. The value on the right is worked out first, then saved under that name.local function addCoins(amount)— This defines a reusable function. Its name describes the job that other code can call.playerState.coins = playerState.coins + amount— This stores or updatesplayerState.coins. The value on the right is worked out first, then saved under that name.end— This line supports one focused piece of application logic. Read it together with the block directly around it.addCoins(5)— This line supports one focused piece of application logic. Read it together with the block directly around it.print(playerState.coins)— This is the visible output line. It shows the final value after the earlier work is complete.
What the output means
15
The output is evidence that the program followed the instructions. If your result is different, read from the first line and write down how each value changes. That is debugging, not failure.
Mistake example
local playerState = nil -- the same state is stored in more than one place
local function addCoins(amount)
playerState.coins = playerState.coins + amount
end
addCoins(5)
print(playerState.coins)
This version intentionally shows how the same state is stored in more than one place. The changed assignment stores a missing value, or a required line is removed, so later code cannot complete its job safely.
Fixed version
local playerState = {coins = 10}
local function addCoins(amount)
playerState.coins = playerState.coins + amount
end
addCoins(5)
print(playerState.coins)
The corrected version restores the real value or required operation. It fixes the chapter-specific problem: the same state is stored in more than one place.
Common mistakes
- Keeping the same state in several places.
- Changing state without notifying its readers.
- Using app-wide state for a value needed by one small component.
Warning: Change one part at a time. If you change many lines together, it becomes harder to learn which change caused the result.
Real use cases
- Update a Flutter widget after setState.
- Manage React state with hooks.
- Track server-owned player state safely in RedM.
Small real-project example
The server keeps the real coin balance and pushes updates to the owning client over an event.
-- server
local playerCoins = {}
RegisterNetEvent("coins:add", function(amount)
local playerId = source
playerCoins[playerId] = (playerCoins[playerId] or 0) + amount
TriggerClientEvent("coins:update", playerId, playerCoins[playerId])
end)
-- client
RegisterNetEvent("coins:update", function(newTotal)
print("Coins: " .. newTotal)
end)
How the project example works
- The server keeps
playerCoins, the one source of truth for each player's balance. coins:addruns on the server so the amount cannot be forged by a modified client.TriggerClientEventsends the updated total back to that one player only.- The client's
coins:updatehandler only displays the number; it never changes the real balance.
Practice exercise
- Add a reset action.
- Keep one source of truth.
- Explain which code owns the state and which code only reads it.
Tip: If the exercise feels too large, complete only steps 1 to 3. Small working code teaches more than a large unfinished project.
Mini quiz
- What is a source of truth?
- Who is allowed to change the state?
- What causes a stale screen?
How to read AI-generated code
Do not copy AI code first. Read it like a detective. Find the data, follow the changes, and locate the final output. Ask AI to explain a line only after you have made your own guess.
- What data goes in?
- What values are stored?
- What calculation or decision happens?
- What is printed, displayed, saved, or returned?
- What can go wrong?
- Can you rename one value and still explain the code?
RedM safety check
RegisterCommand creates a named command. Its callback receives source (who triggered it) and args (the words after the command). Client code handles the local player's screen and input. Server code owns trusted game state. Never trust prices, rewards, permissions, or item counts sent by a client; check them again on the server.
Before you move on
- I can explain this topic in my own words.
- I can read the small example without AI.
- I can change the example and predict the new result.
- I can find and fix one simple mistake.
- I can name one real project that uses this idea.
Next topic
Next, learn best practices and clean code. Before opening it, explain this chapter out loud in under one minute.
Open the interactive lesson →