What does the "scope" parameter do in GetDataStore?

The sample code makes it look like I can create an arbitrary scope and maybe allow multiple of my games to access the same DataStore. Can I? What are the rules?

Some more documentation would be amazing.

6 Likes

Scope is simply meant to give you extended partitioning power. Think of using it as an extra identifier or a subfolder in a main folder on your filesystem. While name gives you one identifier, the scope will give you a second identifier to use.

Scopes are always in use with DataStores - a default string of “global” will be passed. That meaning, there’s no difference between the following:

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("TestData")
local playerData = DataStoreService:GetDataStore("TestData", "global")

There’s any number of uses for names and scopes that you can set up. It all depends on how you choose to organise your data and what best suits your needs. For example, in one game you may find it more fitting to organise your data by using one name, “Data”, while the scope is used to specify what type of data you’re working with.

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("GameData", "Player")
-- All keys are UserIds and the value is a dictionary of player data
-- e.g. {261, {["Level"] = 1}

local playerData = DataStoreService:GetDataStore("GameData", "Server")
-- All key-value pairs are server configurations and their respective values
-- e.g. {"TestServer", true}

Scoping is a fairly literal term, you’re changing the scope of where you’re looking inside a DataStore which by default is “global”. You can use scope in the same way you use name; passing arbitrary strings there is acceptable. The rules are the same as the name.

Cross-game data requires an external server. DataStores can only be used by the places of a game, but not places of another game. Funny time to use the term, but think of it like this: your DataStores are scoped to a game and its places. This scope is private and cannot be accessed by other games.

14 Likes

`

So, in summary, DataStoreService:GetDataStore("TestData" .. "someScope") is equivalent to DataStoreService:GetDataStore("TestDatasomeScope")

`

5 Likes

In the specific case you just posted, yes those are the same because the strings evaluate to the same thing. You’re only concatenating there, not using the scope argument, so both of them would be looking in the global scope. On the other hand, if you were to write this:

DataStoreService:GetDataStore("TestData", "someScope")

It would be different because now it’s looking into the “someScope” scope of the “TestData” DataStore rather than the “global” scope.