How to use State Bags

ahhhh okay. that makes sense now, thought i was losing my mind. thanks a lot

Thanks for this very useful tutorial.

Hi! I’ve been working with state bags lately and I don’t really understand why the state bag doesn’t work as it should.

If I had to guess, it doesn’t support keys under a given table.

Here is an example:

Here you can see that apparently the two codes are the same but still only the first one works and the second one doesn’t. I don’t really understand why this is the case because the end result should be the same even if the structure is different.

Can anyone tell me why it is not working? Could it be a bug in the system or is it just implemented this way on purpose? Help me if you can, thanks in advance for the info.

RegisterCommand('test',function(source, args,rawCommand)

    --This is method works!
    GlobalState.test = {
        ["data"] = {
            class = {
                tempFunc = function()
                    print("working???")
                end
            }
        }
    }
    GlobalState.test.data.class.tempFunc()

    GlobalState.test = {}
    --This method not working!
    GlobalState.test.data2 = {
        class = {
            tempFunc = function()
                print("not working!!")
            end
        }
    }
    GlobalState.test.data2.class.tempFunc()
    
end)

Because it is not supported, see here: State bags - Cfx.re Docs
If you are setting a table as a global state, you always have to set the whole thing. Otherwise it won’t replicate.

1 Like

Hi! Ohh I knew I missed something I started on this forum article. Thanks a lot now it’s clear why it doesn’t work :slight_smile: Thanks again.

Can this be used in javascript runtime?

Yes, its just Entity(152151).state.set instead of Entity(152151).state:set, access works the same as it would in Lua

Awesome! Is there a way to rename the Player or Entity function to another name? I’m current using both as custom classes and was wondering if this was possible .

Thank you. I was stupid and realized I was using Player and Entity as classes. It’s an old project so I totally forgot.

:smiley: thank you

You can do something like export default { Entity, Player }; in another file and import all from that file.

You can view an example here: https://github.com/nativewrappers/fivem-client/blob/d3963f1558841f528e0957b210b17a5446bdf948/src/models/Player.ts#L61-L63

UPDATE: For someone who has a similar issue: I tried to access the Player State Bags on playerDropped event. At this moment the player is gone and his Player State Bag is cleared!

I got an issue with Player State Bag. I setup the bag “name” with a name as value but on access name is nil:

functions.lua

function cacheIdentity(clientId, name, dob, gender)

    -- only server should know
    Player(clientId).state:set("name", name, false)
    Player(clientId).state:set("dob", dob, false)
    Player(clientId).state:set("gender", gender, false)
    
end

server.lua

AddEventHandler("ssv_core:sessionCreated", function(clientId)

    local userId = Player(clientId).state.userId
    
    loadIdentity(userId, function(result)

        if #result > 0 then
            
            local data = json.decode(result[1].data)

            cacheIdentity(clientId, userId, data.name, data.dob, data.gender)
  
            TriggerClientEvent("ssv_identity:spawnUser", clientId)
  
        else
    
            TriggerClientEvent("ssv_identity:showRegisterForm", clientId)
        
        end
    end)
end)

AddEventHandler("ssv_core:sessionDestroyed", function(clientId, userId)

    local name = Player(clientId).state.name
    local dob = Player(clientId).state.dob
    local gender = Player(clientId).state.gender

    print("name->" ..  name)
    
    updateIdentity(userId, name, dob, gender, function(successBool)

        if successBool then
            exports.ssv_core:log(("Daten erfolgreich gespeichert in ssv_identity für [%s]"):format(name))
        else
            exports.ssv_core:log(("Fehler beim Speichern der Daten in ssv_identity für [%s]!"):format(name))
        end
    end)

end)

On this line inside server.lua i get following error:

print("name->" ..  name)
[ script:ssv_identity] SCRIPT ERROR: @ssv_identity/server/server.lua:36: attempt to concatenate a nil value (local 'name')

If i print the name right after the caching method i get the value. But inside the EventHandler(“ssv_core:sessionDestroyed”) the value for name is nil. Idk why. I already added a StateBagChangedHandler and there is only one change fired when i set the value inside the caching method. Im confused. What am i doing wrong?

Is it possible to get all the player’s state bags?
With a for or other think ?

Is it possible to get all the player’s state bags in client side?
With a for or other think ?

@SensationXYZ
@AvarianKnight
@BennoBaba
@nta

thank you for the guide! im so the community will love it!

1 Like