How to make a script that makes a global table if there isn't one yet and then add its parent to it?

I have been trying to make a script that searches if there is a global table (Network for example), and if there isn’t one, the script will create one and add itself to it, but if there is one, the script will just add its parent to it. I could not make this because of limited reference of _G on the developer hub.

Any help or better suggestions to make this work would be very appreciated!

1 Like
If not _G.MyTable then
      _G.MyTable = {}
      table.insert(_G.MyTable,script)
else
      table.insert(_G.MyTable,script.Parent)
end
2 Likes

You really shouldn’t be using _G if you can help it. If you need a table to be used across scripts, use a ModuleScript. It’s generally seen as a bad practice to use the _G table at all, since it might be unintentionally affected by other scripts and it’s hard to debug where its changes are coming from.

6 Likes

I am making a model that makes a network between models like it, so I have no idea how i would implement modulescripts in this case

About the same way you’d use _G but requiring a module from a mutually accessible location (e.g. ServerScriptService). Gets rid of the global bit, retains functionality, accessible by all except the client.

1 Like

that would not be possible in my case, I will have to stay with _G :v

1 Like