Weird issue with GlobalState

Hi guys, so, here’s the issue I’m having ( I’ll try to explain it the best way i can )

What I’m doing is : When a player connects to the server I’m adding a table containing some data inside the GlobalState.players ( set to {} at the start )

Looks like this :

GlobalState.players = {}

-- its setter
SB_GLOBAL.SetPlayers = function(players)
    Citizen.CreateThread(function()
        print('Dumping player being added :')
        print(SB.DumpTable(players))
        GlobalState.players = players
        local data = SB_GLOBAL.GetPlayers()
        print('Dumping Global after :')
        print(SB.DumpTable(data))
    end)
end
-- its getter
SB_GLOBAL.GetPlayers = function()
    local players = GlobalState.players
    return players
end
-- when a player joins it triggers this fonction below
SB_GLOBAL.AddPlayers = function(source)
    Citizen.CreateThread(function()
        local players = GlobalState.players -- getter
        players[source] = { Id = source, Name = "The player name", Coords = vector3(0, 0, 0) }
        print('Player added')
        SB_GLOBAL.SetPlayers(players) -- setter
    end)
end

And it works like a charm, the table is added and i can see/use/get the data of each player
Then, when the player leave ( onPlayerDropped ) I’m removing the table associated with the player

Looks like this :

SB_GLOBAL.RemovePlayer = function(id)
    Citizen.CreateThread(function()
        local players = SB_GLOBAL.GetPlayers() -- getter
        for i = 1, #players do
            if players[i].Id == id then
                players[i] = nil
            end
        end
        print('Player removed')
        SB_GLOBAL.SetPlayers(players) -- setter
    end)
end

And again, this works perfectly, the player is removed and the GlobalState.players is empty ( - actually set back to {} - if for example only one player was online )

My issue is this;

When a player leaves and comes back WITHOUT SERVER REBOOT, the function used to add the players works BUT it seems like the GlobalState.players stays at {} ( example in this case only one players joins/leaves ) even though I print the data inside GlobalState.players and it’s NOT empty. I also have a loop going through the bag to see its size, on the first join it’s showing 1 but then on the second join it stays at 0

Citizen.CreateThread(function()
    while true do
        players = SB_GLOBAL.GetPlayers() -- getter
        print('size : ', #players)
        Wait(500)
    end
end

Screens below :
First join ( server empty > player joins > is added > dumping to see > loop showing size )
image

Removing the player, size gets updated, all good
image

And here the issue
image

I’m losing my mind on this, I can’t explain WHY it’s not updating the GlobalState when it clearly updates it

Important note : it does the same thing if i restart the resource when I’m in-game, the GlobalState.players is not working BUT if i restart it a second time, everything goes back to normal

– I cant send any more code than this since it’s part of my framework