Which datastore habit is better?

I’m wondering which habit of using datastore is better.

As long as i’ve been using this forum i saw something like that (focus on GetAsync):

local DataStoreService = game:GetService("DataStoreService")
local PlayerDataStore = DataStoreService:GetDataStore("PlayerDataStore")
--here is a rest of the code, like pcall etc.
local playerData = PlayerDataStore:GetAsync(player.UserId.."/"..scope)

but in documentation there is something like second argument in GetDataStore which is declaring scope.

local DataStoreService = game:GetService("DataStoreService")
local PlayerDataStore = DataStoreService:GetDataStore("PlayerDataStore", "Scope")

local playerData = PlayerDataStore:GetAsync(player.UserId)

I was thinking what’s better - use scope from GetDataStore (like above) or use long string with scope like PlayerDataStore:GetAsync(player.UserId.."/"..scope).

I personally would just use a different datastore name instead of using a scope. Never used scope in my life. And for the keys, I use the UserId and concatenate something like “Data” at the end of it.

1 Like

I usually just do this:

local DataStoreService = game:GetService("DataStoreService"):GetDataStore("PDS")

-- Rest of the script
local GA = DataStoreService:GetAsync("Player_"..player.UserId, scope)
2 Likes

so how does your update/setAsync look like then?

Exactly how the GetAsync looks

local DataStoreService = game:GetService("DataStoreService"):GetDataStore("PDS")

-- Rest of the script
local SA = DataStoreService:SetAsync("Player_"..player.UserId, scope, Value) -- "Value" is a value that i want to set

like that? that ,scope, Value looks odd to me and i don’t know if it’ll work, but somehow i have to pass what i want to set into this datastore

Sometimes i do something like this:

local RV = Instance.new("IntValue", leaderstats)
RV.Name = "RandomValue"
RV.Value = Ds:GetAsync(plr.UserId, RV.Value) or 1

RV:GetPropertyChangedSignal("Value"):Connect(function()
Ds:SetAsync(plr.UserId, RV.Value)
end

Currently when doing :ListKeysAsync() or :ListDatastoresAsync() you can only filter using a prefix, not a suffix. So doing :GetAsync(player.UserId.."/"..scope) would be useless.

So your only real options are either

local PlayerDataStore = DataStoreService:GetDataStore("PlayerDataStore", "Scope")
-- or
local PlayerDataStore = DataStoreService:GetDataStore("Scope_PlayerDataStore")

-- (and prefixes in keys)
PlayerDataStore:GetAsync("Player_".. player.UserId)

(btw you shouldn’t use / in keys nor datastore names, because when you set AllScopes to true the / is used for datastores that do have a scope (as a seperate parameter))

1 Like

I think that explain (almost) everything to me what i wanted to, thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.