What exactly is a DataStore Scope?

I was reading the Developer Hub about Datastores since there is nothing to do, and I found something called Scopes. Does this mean that I can do

local Money = DataStoreService:GetDataStore("Inventory", "Money")
local Bounty = DataStoreService:GetDataStore("Inventory", "Bounty")

Money:SetAsync("Player_".. UserId, 1000)
Bounty:SetAsync("Player_".. UserId, 20)

Instead of needing to use tables? Or what exactly are they if I am wrong.

Thanks, WE

2 Likes

No — and you should be using tables regardless because it amounts to less SetAsync and GetAsync calls.

Ok. What exactly are DataStoreScopes even though I use DataStore2?

It’s essentially another folder under the Datastore, it doesn’t serve any purpose besides organization. The code sample you provided would presumably work but is very unnecessary.

Every key in a data store has a default “global” scope, but you can further organize keys by setting a unique string as a scope for the second parameter of GetDataStore() ; this will automatically prepend the scope to all keys in all operations done on the data store.

The scope categorizes your data with a string and a separator with “/”, such as:

Key Scope
houses/User_1234 houses
pets/User_1234 pets
inventory/User_1234 inventory

The combination of datastore name + scope + key uniquely identifies a key and all three values are required to identify a key if it has a scope. For example, a global key named User_1234 can be read as follows:

local DataStoreService = game:GetService("DataStoreService")
local inventoryStore = DataStoreService:GetDataStore("PlayerInventory")
local success, currentGold = pcall(function()
	return inventoryStore:GetAsync("User_1234")
end)

By contrast, if key User_1234 has a scope of gold , you can only read it as:

local DataStoreService = game:GetService("DataStoreService")
local inventoryStore = DataStoreService:GetDataStore("PlayerInventory", "gold")
local success, currentGold = pcall(function()
	return inventoryStore:GetAsync("User_1234")
end)
1 Like

So by reading this, my original theory as a “alternative to tables” was correct?

This is from the official documentation page under the heading “Scope”.

But yes, this is a way of having multiple keys per single DataStore table which are representative of separate things. Essentially like multiple tables within the DataStore table itself.

Ok. Last question. Which is more reliable and efficient? Using scopes, or tables?

Multiple scopes within a single DataStore or multiple DataStores? The former would be more efficient, make sure you’re aware of the limitations imposed on any given DataStore & not to supersede them.

No, I am talking about if you should store multiple stuff within 1 datastore with tables or scopes.

Oh, I misunderstood your question, my apologies, I’m not sure if there are any performance differences between the two, however I’d assume scopes are likely the better choice (performance wise) since you can still access the value associated with a key for any given DataStore & some scope directly, whereas if the value is stored within a table, first you’d have to get that table value from the DataStore and then you’d have to index it for the particular value(s) you need.

1 Like