Entity state bags are not replicated from client to server

Client: Canary
Server: 3874 (latest at time of writing.)
OneSync: Yes (+set onesync on)
Game Builds: 1604, 2060, 2189

Expected Outcome:

Setting a state bag on a new, owned entity on the client with the replication flag true as described in State bags, the state bag is replicated to the server.

Actual Outcome:

The state bag is not replicated to the server.

Example Code:

Client:

local object = CreateObject(`prop_alien_egg_01`, GetEntityCoords(PlayerPedId()), true, true)

Entity(object).state:set('foo', 'bar', true)

print('Set \'foo\' State Bag', Entity(object).state.foo)
-- Output of second parameter is "bar".

Server:

for _, entityId in pairs(GetAllObjects()) do
    if GetEntityModel(entityId) == `prop_alien_egg_01` then
        print('Alien Egg Found', entityId, Entity(entityId).state.foo)
        -- Output of third parameter is nil.
    end
end

Repro Resource: statebagtest.zip (991 Bytes)

To reproduce, start the resource and enter the command teststatebag on the client. Relevant output will be printed from both the client and server.

This might be of help ^^

That’s the issue - design limitation at this point since state is a sideband similar to events: the server won’t know of the entity yet until later on, as the state update gets sent too early oftentimes.

I ran into this issue too. I just had a workaround where I manually send it to the server to get updated.

  local veh = Entity(vehicle)
  veh.state:set("nitrousLevel", level, true)
  Citizen.CreateThread(function()
    Wait(500)
    -- Set server
    TriggerServerEvent("setNitrous", VehToNet(vehicle), level)
  end)
AddEventHandler("setNitrous", function(netId, level)
  local veh = Entity(NetworkGetEntityFromNetworkId(netId))
  veh.state.nitrousLevel = level
end)

This seemed to resolve the issues for me, but it is a hacky workaround.

What is the point of state bags if it don’t get replicated to server? The main reason for using state bags is not wanting to use callbacks or events to sync data.

Player state bags are getting replicated. Just Entity’s are not getting replicated. Idk if that’s design or bug.

It does, though, and the topic here also provided a workaround: wait until the server knows of the entity before setting the state value.

What is the point of complaining ‘what is the point’ on a 5 month old topic instead of providing a constructive question with code/other actionable things?