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

What is a data table and _G.data? Can someone explain this clearly?

if not _G.data then -- Create the data table
_G.data = {}
end

I tried to find an explanation and found another script example. But still, I can't understand what they do and how they work:

G.Data[player.Name]["Upgrades"] = {
		 -- value, valueIncrement, max/min, cost, costIncrement
		["CoinsPerCritical"] = {1, 1, 100, 25, 25};
		["Chance"] = {1, 1, 100, 100, 100};
		}

2 comments

0

_G are global variables that you can share between scripts. I don't know if people still use these now that we have Remotes and Modules. In this case _G.Data is a global table that is storing a players name as the Key, Upgrades as the subtable Key and the Value is the table of upgrades.

0

Yes I still use that, because module script have these utility and _G too. When I make a Database I use _G, when I make modes I use ModuleScript. ModuleScript isn't really the best to made a Database... Just to simplify the first real code that use _G lol. The big problem with a module is you need to found this. But a _G you just need to wait that let it be create by the owned variable script.

2 answers

Second one looks like a module script named G which returns a table named data.

_G looks like a global variable read about it here

Module scripts are generally better to use than _G. _G is slow since it has to look through a lot of places for the variable.

Module scripts are scripts that return ONE variable when you require them. Usually they return a table with variables/functions inside them.

#Example: Lets say I have a module script named myModule in the replicated storage

Here is what myModule could look like:

local module = {}

module.myVariable = 2
module.myVariable2 = "this is a string"


function module.printMyVariables()
	print(myVariable, myVariable2)
end

return module --return a table with all the variables/functions above ^^^

In a normal script, I could require() my module script and use all the variables and functions inside of it:

local myModule = require(game:GetService("ReplicatedStorage").myModule)

myModule.printMyVariables()
print(myModule.myVariable, myModule.myVariable2)

Where you can read about modulescripts: https://developer.roblox.com/en-us/api-reference/class/ModuleScript

2 comments

0

i read the question wrong, i think

0

pls respond

_G or shared is a table that is shared across scripts of the same context levels. So for example, if you did _G.hi = "hi" in one script, and in another did print(_G.hi), as long as the script that sets _G.hi runs first, the second one will print "hi".

Note that _G on the client is unreliable, as exploiters can change the data within the table very easily. I wouldn't recommend putting sensitive stuff in _G or stuff you need to rely on in _G for the client.

1 comment

1

The client being able to modify _G shouldn't compromise your security.

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