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.