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

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

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")`

7 comments

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.

0

and thats not how you get a player's data

0

really don't do that @xMonaitary. Use modules or what radusavin366 said

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.

0

Oh, well I knew that, I just thought this would be easier, thanks.

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.

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

1 answer

✓ Accepted answer

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.

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