Storing a table with _G

Hello!

I am trying to store a table within the _G table. Currently I was doing:

local squads = _G.SquadsList

_G.SquadsList = {}

table.insert(squads, player.Name)

However I got an error:

Players.LightBoltex.PlayerGui.MenuScreen.SquadInvite:27: invalid argument #1 to 'insert' (table expected, got nil)

Any solutions?

1 Like

You’re assigning squads to _G.SquadsList before you actually create the table. The squads variable isn’t going to change automatically. You need to swap those two lines:

_G.SquadsList = {}
local squads = _G.SquadsList

table.insert(squads, player.Name)

Also, as a general rule-of-thumb, using _G is typically considered bad practice. It would be better to throw your SquadsList into a ModuleScript instead.

6 Likes