How do I use _G?

Hi, I have a question about _G.
I tried to use it.

game.Players.PlayerAdded:Connect(function(Plr)
	_G.PlayerWeapon = 0
end)

Am I right?

But I have a question. Is it can only use in the local script?

_G is used as a global table that can be used in server scripts and localscripts, the contents stored in _G can be viewed in any script but the contents depends on the side it was set in, say I do

_G.bread = 10

In a server script and I try to print it from a localscript, it would print nil since it was set on the server and vice versa if you do it in a localscript and print from a server script.

As for what you are doing, it’s going to overwrite the key everytime a player joins, I think you meant to create a key with the player instance judging by your implementation

game.Players.PlayerAdded:Connect(function(Plr)
	_G.PlayerWeapon = {}
    _G.PlayerWeapon[Plr] = 0
end)

Although I think it would be better to use a different approach as people have discouraged the use of _G, but you cna still use it if you want

1 Like

_G. is basically a global variable

So if you set _G.Variable = true in 1 script and then after you set that you can get _G.Variable from another script and it will return as true

These values will only be set on their respective side, for example:

LocalScript:

_G.Variable = true

Other localscripts will see this
Server scripts will not see this

And vice versa for server scripts