I am currently making my data store for my game, and there are some conflicting options. On the Roblox API site, it uses the player.UserId as the scope in:
DataStoreService:GetDataStore(name, scope)
However, I’ve always used the Player.UserId as the key to access stuff within such a scope. Which returns me a table of data.
So for example:
local lvls = DataStoreService:GetDataStore("Version1", "Levels/Experience")
local l = lvls:GetAsync(plr.UserId)
plr.leaderstats.Level.Value = l["Level"]
plr.leaderstats.Exp.Value = l["Experience"]
That is what I do.
But Roblox API does:
local function onPlayerAdded(player)
local scope = "Player_" .. player.UserId
local playerStats = DataStoreService:GetDataStore("Stats", scope)
local playerResources = DataStoreService:GetDataStore("Resources", scope)
end
My question is: What is the more efficient/faster method of using data stores? Using the UserId as the key or as the scope?
Note: I realize this might be dependent on scripter’s structuring of the data store.
Changing the scope (internally) makes a new datastore. You shouldn’t have player-specific datastores or scopes.
This is because datastores are cached under the DataStoreService for the lifetime of the game server (and cannot be removed from said cache). This means that having a number of unique players will be detrimental to the performance of your game.
You really should use keys and store all the data in a table, under said key.
I agree with what ElliottLMz said - using scopes adds to the number of datastores. Stick with one key as you are already doing, and you can much easily get the data from the table you use, change it easily and reorganize it as you like.
However, I would like to know what the purpose of RemoveAsync is if it’s purpose is to remove a data store? Is it not removing the cache? The wiki doesn’t get into the specifics, so I was curious after reading this thread.
I’m confused then, as to why the Roblox API recommends (or at least shows) using the player user id as the scope. This could instigate bad habits in new scripters. Is the page an old one?