Too Many DataStore Requests, How Can I Fix?

I’m trying to save some boolValues and intValues to the player, but I keep getting this error saying that I’m sending too many requests. How can I make it so fewer requests are sent? Thanks.

local ds = game:GetService("DataStoreService"):GetDataStore("CardSaving")
local cards_folder = game.ServerStorage.Cards

game.Players.PlayerAdded:Connect(function(plr)
  local cards_fold = Instance.new("Folder")
	  	cards_fold.Name = "Cards"
  	  	cards_fold.Parent = plr
	
	for _, v in pairs (cards_folder:GetChildren()) do
		
		local card = Instance.new("Folder")
		card.Name = v.Name
		card.Parent = cards_fold
		
		local cardOwned = Instance.new("BoolValue")
		cardOwned.Name = "CardOwned"
		cardOwned.Parent = card
		
		local cardAmount = Instance.new("IntValue")
		cardAmount.Name = "CardAmount"
		cardAmount.Parent = card
		
		local lvl = Instance.new("IntValue")
		lvl.Name = "Level"
		lvl.Parent = card
			
			if ds:GetAsync(cards_fold.Parent.UserId .. plr.UserId) ~= nil then
				local succ, msg = pcall(function()
					cardOwned.Value = ds:GetAsync(card.Parent.. plr.UserId)
					cardAmount.Value = ds:GetAsync(card.Parent.. plr.UserId)
					lvl.Value = ds:GetAsync(card.Parent.. plr.UserId)
				end)
				if not succ then
					warn("Problem with getting and setting data "..msg)
				end
			end	
	end
end)
		

game.Players.PlayerRemoving:Connect(function(plr)
  local succ, msg = pcall(function()
	for _, v in pairs (plr.Cards:GetChildren()) do
			local card = v
		for _, v in pairs (card:GetChildren()) do
			
				ds:SetAsync(v.Name .. plr.UserId, v.Value)
		end
	end
  end)

  if not succ then
    warn("Problem with saving data ".. msg)
  end
end)

I suggest you just use DataStore2, because it’s way easier to save that way, and eliminates that request problem your having

1 Like

I’m not too experienced with regular dataStores, do you think I should fully learn dataStore before learning dataStore2?

It think it’s recommended that you familiarize yourself with DataStore itself and its limitations before you try DataStore2. I’d give some video tutorials on DataStores a watch, like @Alvin_Blox.

Well, the issue with your code is your saving each card under a separate key. Instead you should be saving a table with all the cards in one table. Switching to DataStore2 wouldn’t fix the issue since you aren’t efficiently saving the data with your current DataStore system.

for _, v in pairs (card:GetChildren()) do
			
				ds:SetAsync(v.Name .. plr.UserId, v.Value)
		end

The issue is you are are saving each thing seperate so instead do something like this:

local data = {}
for _, v in pairs(card:GetChildren()) do
    table.insert(data,v.Name,v.Value)
end
ds:SetAsync(plr.UserId,data)

This is why it’s better practice to keep player data to be saved in tables inside of ModuleScripts since it will make it a lot easier to manage the data.

2 Likes

There is a fairly simple way and always ensure data for a certain period of time (I’m usually 5s).
You only need to get the change event (it must be an Instance Value) and wait () a period of time before saving the changes.
Here is the code for you

local value= game. -- you should reference to value
local waitSave=false
value:GetPropertyChangedSignal("Value"):Connect(function()
	if waitSave then 
		return
	end
	waitSave= true
	wait(t) --time you want here
	DataStore:SetAsync(key,value.Value)--save data
	waitSave=false
end)