Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Are there any downsides to using "_G." for storing Data on the server?

I've been looking into making my game more efficient, and I found out about making Global tables/functions. From what I can tell, I'll be able to load a player's data in one server script:

_G.Stats = {}

Players.PlayerAdded:connect(function(Player)
	_G.Stats[Player.Name] = DS:GetAsync(Player.UserId) or {hi = true}
end)

and then be able to access that data from another server script:

_G.Stats[Player.Name]["hi"] = false

Are there any downsides to doing this? Right now, player's stats load in one script and other scripts needing these stats retrieve it through bindable functions. Thanks!

5 comments

1

I do believe _G. is better than bindable functions - just remember that _G. does not transfer between server and client (or vice versa).

0

Alright. For how I want to use it, it's beneficial that _G. doesn't transfer between the server/client as I'm hooking my save system up to it. I'd prefer for the client to not edit their own stats ;)

2

I'd sure hope _G is efficient. ROBLOX stores tons of stuff in _G.

0

Have an upvote @shayner32. Somehow that made me chuckle.

0

_G is bad style, you should consider using ModuleScripts instead

1 answer

_G is bad style.

One of the principle ways to make your code better is to reduce the scope of everything as much as possible -- when fewer things are at hand, everything becomes less complex.

With _G, every object can peek into every other object's state. That's particularly problematic if two different, unrelated scripts accidentally blunder into using the same keys.

Basically, writing maintainable code using _G is not really possible.


ModuleScripts are a great alternative. They let you box up functionality into functions and state, which lets you have a clean interface for sharing between functions.

You could even use them as a drop-in-replacement for _G by just making them

-- Stats modulescript
return {}

You can easily just say

local Stats = require(game.ServerStorage:WaitForChild("Stats"))

....

Stats[Player.Name] = DS:GetAsnc(.....

though again, I recommend building abstractions and interfaces rather than just throwing data into a table.

Really, the ModuleScript can do a lot of work for you to make your life easier. Think about how you'd like to write code, and then make it possible by designing a nice ModuleScript that behaves that way.

2 comments

0

Thanks for the reply, but in my case I feel as though using a ModuleScript to handle my Stats just won't work. I've instead set my game up as to where all of my data loads / saves in a single module script, and is then placed in _G tables. I understand why it might seem inefficient, but nearly all of my server-side scripts need quick access to the data, so as far as I can tell, this is currently t

0

I don't understand why it "just won't work" -- you have easy access via the table a module returns

This discussion is preserved as it was. New answers, comments, and votes are closed.