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

Is global variables good?

Hello, so.. i use global variables for all the player's data, with a dictionary:

_G.data = {}

game.Players.PlayerAdded:Connect(function(player)
   _G.data[player.userId] = UserData:GetAsync(tostring(player.userId)) or 
   {
       money = 0, lvl = 0, xp = 0, stage = 1
   }

end)

everything is in this global variable, but i want to know if it's good to use or sometime it'll break. and if it does break sometimes, should i use Bindable Events to call the data?

something like

in main:

data = {}

game.Players.PlayerAdded:Connect(function(player)]
   data[player.userId] = UserData:GetAsync(tostring(player.userId)) or 
   {
       money = 0, lvl = 0, xp = 0, stage = 1
   }

end)

events:WaitForChild("getData").OnServerInvoke = function(player)
return data[player.userId]
end

in some script:

local data = events:WaitForChild("getData").InvokeServer(player)

...

1 answer

✓ Accepted answer

Well using _G will take longer than using things returned by a module.

Tomarty explains this better on devforum here

ModuleScripts are reportedly faster than using global variables and more efficient too, since global variables can only be accessed by the basic type of scripts however modules can be access by any script

1 comment

0

Thanks, that helped

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