Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is global variables good?

Asked by 5 years ago

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

Log in to vote
1
Answered by
Arkrei 389 Moderation Voter
5 years ago
Edited 5 years ago

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

0
Thanks, that helped Caniisu 2 — 5y
Ad

Answer this question