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

How does the name and scope in GetDataStore work?

Asked by
Sorukan 240 Moderation Voter
5 years ago
Edited 5 years ago

So there's 2 things i want to know, what is the purpose of the datastore name and what is the purpose of the scope? I have this game where i added multiple places in and i noticed that when i create a datastore script in each place, the data saves to every single place as long as the name is the same. I know how to save data across multi place games but it seems unnecessary since the data will load as long as i use the same datastore name in every place. As for scope, i'm still unsure what it does and how it can be useful.

1 answer

Log in to vote
1
Answered by 5 years ago

The scope can be very useful for organizing data. The name of the datastore is the actual identifier used to retrieve it. There can be multiple data stores under the same name but with different scopes. For example, let's say I have a data store for player data, and I want to store the player's money and level. Instead of having a "PlayerData" datastore that stores tables each with a value for money and level, I could instead have a "PlayerData" store in the scope of a unique identifier based on the user's ID with distinct keys for the money and level

--Without scope
local ds = DataStoreService:GetDataStore("PlayerData") -- scope defaults to "global"
local data = ds:GetAsync("plr_" + player.UserId) --data will be in the form {money, level}

--With scope
local ds = DataStoreService:GetDataStore("PlayerData", "plr_" + player.UserId) -- the datastore is distinguished by the player's unique id
local money = ds:GetAsync("Money");
local level = ds:GetAsync("Level");

I probably got some of the syntax wrong since I haven't used this service in a while, but hopefully I made the idea clear. That's just the way I've used scope in the past; I've seen many other usage cases such as:

Distinguishing between places in a multi place game (maybe you have a racing game and you want to keep track of individual high scores on each map)

Having unique leaderboards for each month (in this case you would use ordered data stores, and use the month as the scope)

That's all I can say about scope; I hope you see some use cases for this now. I personally didn't know a single thing about it until I saw a friend use it in his code, and since then I have always utilized it when working with DataStoreService.

Ad

Answer this question