fivem
fivem copied to clipboard
AddStateBagChangeHandler, entity does not exists when event is triggered
Hello,
When using the following state bag handler on, a vehicle
AddStateBagChangeHandler('bubble', nil, function(bagName, key, value, reserved, replicated)
local netId = tonumber(string.match(bagName, 'entity:(%d+)'))
local vehicle = NetToVeh(netId)
print(vehicle)
end)
I get the following error: GetNetworkObject: no object by ID 27 0
But the following works fine:
AddStateBagChangeHandler('bubble', nil, function(bagName, key, value, reserved, replicated)
Citizen.Wait(0)
local netId = tonumber(string.match(bagName, 'entity:(%d+)'))
local vehicle = NetToVeh(netId)
print(vehicle)
end)
I would expect the entity to exist when the handler is triggered.
Repro:
server:
for _, veh in pairs(GetAllVehicles()) do
Entity(veh).state.bubble = 4
end
client:
AddStateBagChangeHandler('bubble', nil, function(bagName, key, value, reserved, replicated)
local netId = tonumber(string.match(bagName, 'entity:(%d+)'))
local vehicle = NetToVeh(netId)
print(vehicle)
end)
Then leave the onesync zone of the vehicle and enter it again
Also the Player states are not already updated when the handler is triggered.
Client:
AddStateBagChangeHandler('bubble', nil, function(bagName, key, value, reserved, replicated)
print(LocalPlayer.state.bubble)
Wait(0)
print(LocalPlayer.state.bubble)
end)
Server:
Player(1).state.bubble = 4
Results:
nil
4
I would expect the LocalPlayer state to be already updated when the event is triggered
I would expect the LocalPlayer state to be already updated when the event is triggered
Why? The new value is stored in value
after all, so perhaps it would be better to let the old value be accessible at that point.
I would expect the LocalPlayer state to be already updated when the event is triggered
IIRC the design was to offer rejection of statebag changes, although not implemented (needs confirmation), it explains why it's not updated at that point.