State handlers (tm.addStateHandler)
Client side only. A wrapper around AddStateBagChangeHandler for players and entities that waits for the entity to exist locally and gives your callback a real entity handle instead of a bag name.
lua
local cookie = tm.addStateHandler('disabled', function(entity, netId, value, bagName, replicated)
if value then
SetVehicleEngineOn(entity, false, true, false)
end
end)tm.addStateHandler(filter, cb)
| Argument | Type | Description |
|---|---|---|
filter | string? | State bag key to watch. nil watches every key. |
cb | function | Called as cb(entity, netId, value, bagName, replicated). |
Returns the handler cookie from AddStateBagChangeHandler, which you can pass to RemoveStateBagChangeHandler.
Callback arguments:
| Argument | Description |
|---|---|
entity | Local entity handle. For a player:<serverId> bag this is that player's ped. |
netId | Network id for entity:<netId> bags, otherwise nil. |
value | The new value. |
bagName | The bag that changed, for example entity:1234. |
replicated | Whether the change was replicated from the server. |
Behavior
- It waits one frame for the bag update, then polls every 50 ms for up to 10 seconds for the entity to exist. If it never shows up the change is dropped and a warning is logged through
tm.log. - Only
player:,localEntity:andentity:<netId>bags are handled. Any other bag (for exampleglobal) is ignored. - Each pending change uses its own waiting thread, which ends as soon as the entity exists or the wait runs out.
Compared with AddStateBagChangeHandler
The native handler only gives you the bag name and fires whether or not the entity exists yet. tm.addStateHandler resolves the entity for you, so you do not parse bag names or check existence yourself.