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

Would I be able to use _G to share a data store between scripts?

Asked by
B_rnz 171
5 years ago
Edited 5 years ago

Hello, my name is xMonaitary and I am new to this website. I found out that this website is very helpful and decided to make my own acc here.

My question is: Would I be able to share datastores between two scripts using _G function?

local DataStore = game:GetService("DataStoreService")
_G.PlayerData = Datastore:GetDataStore("Randomuserdatav1.1")`

0
while you can, i dont suggest it, instead use only 1 script that takes care of the data and sends it to other scripts through remote/bindable events/functions. radusavin366 617 — 5y
0
and thats not how you get a player's data radusavin366 617 — 5y
0
really don't do that @xMonaitary. Use modules or what radusavin366 said User#19524 175 — 5y
0
Yes you can ,but you don't need _G. Your datastore can be accessed in multiple scripts, even in places within the same game universe, by just doing the same thing in the other script - you'll get the datastore with the same name. Rare_tendo 3000 — 5y
View all comments (3 more)
0
Oh, well I knew that, I just thought this would be easier, thanks. B_rnz 171 — 5y
0
And @radusavin366, I was using folders and string values as the player's data. @incapaz, I have absolute no idea how to correctly use dictionaries.. in a module especially. B_rnz 171 — 5y
0
That's not how to save/store data, and while dictionaries are useful (especially when you have a lot of data), they're not *needed* for datastore. Using them in a module is no different. Make a function in a module that gets data, and one that sets data. Make a server-side copy of the data if you really want to & only save when the player chooses / when they leave if you're worried about throttlle vanilla_wizard 336 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

To answer your question directly, yes. You can technically use _G. To answer the more important question of whether or not you *should,* not really.

First, any server script is allowed to check DataStore. You don't need _G for that (though you also shouldn't constantly send get requests or overwrite existing data, you'll get throttled.)

You should use module scripts. Use module scripts both to easily check and set data in the DataStore, and to have server-side variables that can be checked and modified from other scripts. _G isn't necessary here, and this isn't the best application for it. It's often regarded as a poor practice today. (A lazy solution is to use value objects, but I don't recommend them.)

You might be interested in the following pages:

ModuleScripts

Data Store

Saving Player Data

These pages provide both examples and descriptions which should help you.

Ad

Answer this question