Proper way to use DataStores?

Is there a “proper” way to use DataStores? Until now I’ve been defining a single Store and then setting each key to a player’s userId and the value to the player’s data in table form, like this:

local userId = 2291940452

local store = DataStoreService:GetDataStore("main_player_data")
local success, playerData = pcall(function()
    return store:GetAsync(userId)
end)
--[[
playerData would be a table which has all of the player's data, for example:
{
    TotalKnockouts = 7,
    TotalWipeouts = 4,
    HighestKillstreak = 2
}
]]

This has been beneficial for me since I can use different DataStores in-game and when testing. It easily allows you to create multiple save slots for each player and swap between those datastores whenever needed. For example,

DataStoreService:GetDataStore("PlayerData_MainSlot1")
DataStoreService:GetDataStore("PlayerData_MainSlot2")
DataStoreService:GetDataStore("PlayerData_MainSlot3")
DataStoreService:GetDataStore("PlayerData_TestingSlot")

However I’ve seen a couple programmers create a separate datastore for each userId, like this:

local userId = 2291940452

local store = DataStoreService:GetDataStore(userId)
local success, value = pcall(function()
    return store:GetAsync("key")
end)

Their scripts would look something like this:

function getValue(id, key)
    local store = DataStoreService:GetDataStore(userId)
    local success, value = pcall(function()
        return store:GetAsync(key)
    end
    if success then
        return value
    end
end

print(getValue(2291940452, "TotalKnockouts")) -- 7
print(getValue(2291940452, "TotalWipeouts")) -- 2
-- etc.

Is one of these methods considered “proper” and are there any advantages/limitations to each? I’d expect that the first method would be better in scenarios where a lot of information is being logged, since you can get/set everything at once in a single request, while the second method requires you to make a separate call for each value you want to get or set.

First option is easier to program and would most likely perform better since it’s 1 call vs many. I personally use the first method.

2 Likes

Making a GlobalDataStore for each user would defeat the whole purpose of using a datastore. It’s supposedly a storage to hold a lot of values and manage it all within that one storage. Making a storage for each user may not only be decaying performance, but it’s practically unnecessary for someone to do and just makes that portion of code seem messy and unorganized.

Which, then I would say the first method (which was what you were doing before) is the “proper” way of doing it. It’s okay to make test datastores, I just believe you shouldn’t make a lot of datastores for no reason in particular.

1 Like

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