So I am really new to using Datastore2 and am unsure of a couple of things. The main thing I’d like to focus on for this question is how to be sure all values are saved. I have a script that is supposed to save the different types of helmets the player has bought. There are 20 helmets, so the script has to store 20 values per player. (Using the value of zero if they don’t own it, and the value of 1 if they do). However I’ve been getting this error when I tested.
You have probably not combined Your datastores. This causes each value to be stored separately and You are hitting the limits due to too many datastore calls.
Using Datastore2 (by Kampfkarren) requires You to manually combine each store. Do not worry. It is pretty straightforward and needs to be done only once at the start of the server.
I also suggest to keep a table of all Your datastores, to simplify Your code. "DATA" will be Your master key, but feel free to use anything you like.
local Stores = {
"BronzeSphereHelmet",
"GoldSphereHelmet",
"BlueSphereHelmet",
"RedSphereHelmet",
--this is just example, please add more values as needed
}
--COMBINE DATASTORES
for _, store in pairs(Stores) do
DataStore2.Combine("DATA", store)
end
-- using a table can also simplify your player store loading
local PlayerData = {}
game.Players.PlayerAdded:Connect(function(player)
PlayerData[player] = {}
for _, store in pairs(Stores) do
PlayerData[player][store] = DataStore2(store, player)
end
end)
-- always remember to clean up
game.Players.PlayerRemoving:Connect(function(player)
PlayerData[player] = nil
end)
Hi, I am also new with datastore2 and I have difficulty understanding tables. My quick question is that if I would have 30 to 50 keys and combine it into one master key like DataStore2.Combine(masterkey,key1,key2…key50), will I experience errors with this setup? Thankyou.
You shouldn’t if everything else is error free. That is the whole point. It is not about the size of datastores it is about amount of calls to Datastore API. Combined datastores require less API calls, and that makes Roblox staff happy.
Alright, now I can move on with adding new 50 keys in one masterkey instead of doing tables which I am not so sure and not ready if any errors come in. Thankyou, you made me happy!
Wait last question, you said its about amount of calls to Datastore API so if I have 50 keys combined with DataStore2.Combine then at first load of player’s data at playeradded event, does it mean it will call the datastore for 50 times that will make Roblox staff sad (hitting datastore limit) or no?
No. DataStore2.Combine does not actually make any DataStore calls (as far as I know). It is internal DataStore2 function that instructs the module to save everything in one big table.
So the difference of having a table of all your datastores is that it just simplify how you combine all your keys in one masterykey and simplify your players store loading? I am sorry for many questions.
Yes it makes it more readable, but there is not really a functional difference if you do it line-by-line. The real bonus comes in, when you have to reuse it.
I am very grateful that I am knowing every answers to my questions in my mind because of you. And I also want to clarify with the amount of calls to Datastore API if this codes will hit the limit since I am not sure if this will call the Datastore API 50 times because of 50 keys?
DataStore2.Combine(“DATA”,“key1”,“key2”…“key50”)
game.Players.PlayerAdded:Connect(function(player)
local CashDataStore = DataStore2(“key1”, player)
local Cash1DataStore = DataStore2(“key2”, player)
.
.
.
.
.
local Cash50DataStore = DataStore2(“key50”, player)
local folder = Instance.new("Folder")
folder.Parent = player
folder.Name = 'Folder'
--each key has its own instance so 50 instances
local Cash = Instance.new("IntValue")
Cash.Parent = folder
Cash.Name = 'Bobux'
--each instances will have this too
local function cashUpdate(value)
Cash.Value = CashDataStore:Get(value)
end
cashUpdate(0)
CashDataStore:OnUpdate(cashUpdate)