Table within _G unable to be accessed in a function environment

I’m running this code when the player leaves, so that their things can be saved,

game.Players.PlayerRemoving:Connect(function(P)
	SetData(P.Name,P.UserId)
	
	_G.PlayerBackpacks[P.Name] = nil
	_G.PlayerCurrencies[P.Name] = nil
	_G.ActivePlayers[P.Name] = nil
end)

Now, all this does is calls a function which turns the data into a JSON string, and then uploads it, and then clears out the table references so that they can later be garbage collected. However, for some odd reason, within this function’s environment, the table _G.PlayerBackpacks[P.Name] is nil.

Outside of it, it can be read perfectly. Oddly enough, after setting up an incredibly fast wait() loop, it’d end up printing table: 0xa363462dec449deb, as expected, then print nil when I had print statements within that function, and then print another table: 0xa363462dec449deb right after I’d receive the attempt to index nil error. A little something like this (nil, nil marfit are within that function’s environment, 0 is unrelated, everything else is related).

I will most likely not be responding for a few hours, as it is a bit late.

_G.PlayerBackpacks[P.Name] is not nil, you are trying to index a nil object with the .Backpack.

It might be something as simple as trying to use the second parameter of your SetData function instead of the first, but without the SetData function there isn’t much else I can do in terms of advice on fixing the issue.

All of the above is nil and not a table.
It is very unorganized and inefficient to index _G try to use Bindables if you’re trying to communicate between scripts.

The error isn’t here, I need to see the SetData function.

@TheLegendaryDragon99
No, all of the above are not nil and a table. I’m not communicating through scripts here either.
@JarodOfOrbiter
The error would be the exact same, just on a different line, if I were to have print(_G.PlayerBackpack[P.Name].Backpack) The issue is that it’s reading _G.PlayerBackpac[P.Name] as nil instead of the table which should be there (and can be read, as evident with the loop I used to print it out).
@Imp_erator
_G.PlayerBackpacks[P.Name] is nil , it’s what I am printing out when the print statements read nil . However the issue is it only reads nil when in that function’s environment.