LocalPlayer synced State Bag

Something that might be useful is being able to only sync a state bag to the local client, as there’s some data that might only be useful for the local client and doesn’t need to be sent to every other client.

This would be somewhat similar to how it’s currently done but add a 4th option that defaults to false

Player(1).state:set('someUniqueData', 'unique', 
   true --[[replicated]],  true --[[ only sync to local client ]] )
1 Like

What would ‘sync to the local client’ mean in this context - isn’t that what replicated: false is meant for?

‘sync to the local client’ would mean to only Player 1, instead of anyone inside of their scope.

Unless I’m wrong, replicated: false would not replicate any of the data to any player

1 Like

But… if you’re running this code on ‘player 1’, aka ‘the local client’, it will already not ‘sync’ if you’re using replicated: false.

The idea was to allow the server and the client to both have the state bag but none of the other clients would get the data.

… the server isn’t nor does it have a ‘local client’. What’s with the weird phrasing, then?

To recap.

Because this post is confusing as hell and the whole LocalPlayer for the individual client to update the Player state put my head through the ringer.

Client side:


-- This is client 1
-- Only on the 'local' client
-- example 1
LocalPlayer.state:set("This", data, false)

-- Client asks to sync the data
-- example 2
LocalPlayer.state:set("This", data, true)


-- This is client 500
-- from other clients to get "This" data.
Player(1).state.This


  • First example, unless declared on the server prior to change, would be nil as it doesn’t exist on the network.

  • Second example would return the player 1 data if called from another client.

This is what I have been rolling wih the understanding of.

Please correct or school me dbubs.

1 Like
--- bagName
--- value
--- replicated - server and all clients are aware of the value
client: LocalPlayer.state:set('this', data, true)
server: Player(6).state:set('this', data, true)

The idea being to add a fourth parameter which tells the statebag to only sync between the server and a specific client.

-- bagName
-- value
-- replicated - the value is synchronised with the server
-- private - other clients are not aware of this value (default: false)
Player(6).state:set('this', data, true, true)

playerId 6 is able to access LocalPlayer.state.this, but playerId 3 will return nil if attempting to view the value. Say we setup some resource for loading and storing player data (i.e. cfx-server-data playerdata) and want to attach some states to a player that other clients should not or do not need to be aware of.

You could of course just trigger a client event which will set this state with a non-replicated value when it’s modified by the server.

But doesn’t the server do this anyway if we are setting a client’s data?

The false flag is for replication to other clients, not inclusive of the client that the state represents?

EDIT:

No it doesn’t, I thought it would have, but a test provided nil.
Odd though, perhaps the false flag for replication can be changed to only send to the client it represents? Rather than adding a 4th parm?

1 Like

No, what? Changing existing behavior and breaking existing code is silly.