Skip to content

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) ​

ArgumentTypeDescription
filterstring?State bag key to watch. nil watches every key.
cbfunctionCalled as cb(entity, netId, value, bagName, replicated).

Returns the handler cookie from AddStateBagChangeHandler, which you can pass to RemoveStateBagChangeHandler.

Callback arguments:

ArgumentDescription
entityLocal entity handle. For a player:<serverId> bag this is that player's ped.
netIdNetwork id for entity:<netId> bags, otherwise nil.
valueThe new value.
bagNameThe bag that changed, for example entity:1234.
replicatedWhether 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: and entity:<netId> bags are handled. Any other bag (for example global) 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.