Help fixing a datastore request error with datastore2

Basically my game has an inventory system with a lot of different items (Around 40) and I want to be able to save it all more efficiently. I,m currently using datastore2.

My issue is that the method I’m using now sometimes causes an error where it takes forever to load and I get the error “DataStore request was added to a queue. If request fills, further requests will be dropped,. Try sending fewer requests”. I think this is caused by trying to get different data from the same key multiple times.

I’ve looked at many different forums on the topic but I can’t seem to wrap my head around it. How can I make this code more efficient so I don’t get this error? This code is a simplified version of the one I currently have in my game. This one is only loading 4 different pieces of data but the one in my game loads around 40.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")

local DataStore2 = require(ServerScriptService.DataStore2)

DataStore2.Combine(
	"InventoryData1",
	"Mask1",
	"Mask2",
	"Mask3",
	"Mask4"
)

game.Players.PlayerAdded:Connect(function(Player)
	local Mask1Store = DataStore2("Mask1", Player)
	local Mask2Store = DataStore2("Mask2", Player)
	local Mask3Store = DataStore2("Mask3", Player)
	local Mask4Store = DataStore2("Mask4", Player)
	
	local Inventory = Instance.new("Folder")
	Inventory.Name = "Inventory"
	Inventory.Parent = Player
	
	local Mask1 = Instance.new("BoolValue")
	Mask1.Name = "Mask1"
	Mask1.Value = Mask1Store:Get(false)
	Mask1.Parent = Inventory

	Mask1Store:OnUpdate(function(new)
		Mask1.Value = new
	end)

	Mask1.Changed:Connect(function()
		Mask1Store:Set(Mask1.Value)
	end)
	
	local Mask2 = Instance.new("BoolValue")
	Mask2.Name = "Mask2"
	Mask2.Value = Mask2Store:Get(false)
	Mask2.Parent = Inventory

	Mask2Store:OnUpdate(function(new)
		Mask2.Value = new
	end)

	Mask2.Changed:Connect(function()
		Mask2Store:Set(Mask2.Value)
	end)
	
	local Mask3 = Instance.new("BoolValue")
	Mask3.Name = "Mask3"
	Mask3.Value = Mask3Store:Get(false)
	Mask3.Parent = Inventory

	Mask3Store:OnUpdate(function(new)
		Mask3.Value = new
	end)

	Mask3.Changed:Connect(function()
		Mask3Store:Set(Mask3.Value)
	end)
	
	local Mask4 = Instance.new("BoolValue")
	Mask4.Name = "Mask4"
	Mask4.Value = Mask4Store:Get(false)
	Mask4.Parent = Inventory

	Mask4Store:OnUpdate(function(new)
		Mask4.Value = new
	end)

	Mask4.Changed:Connect(function()
		Mask4Store:Set(Mask4.Value)
	end)
end)
2 Likes

I may have solved the issue. Would this code be more efficient and send fewer requests?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")

local DataStore2 = require(ServerScriptService.DataStore2)

local ItemTable = {
	{
		Name = "Mask1",
		Value = false,
	},
	{
		Name = "Mask2",
		Value = false,
	},
	{
		Name = "Mask3",
		Value = false,
	},
	{
		Name = "Mask4",
		Value = false,
	},
}

DataStore2.Combine("MaskData", "Masks")

game.Players.PlayerAdded:Connect(function(Player)
	task.wait(10)
	print("Started")
	local MaskData = DataStore2("Masks", Player)
	
	local PlayerTable = MaskData:Get(ItemTable)
	
	local Inventory = Instance.new("Folder")
	Inventory.Name = "Inventory"
	Inventory.Parent = Player
	
	for i, v in pairs(PlayerTable) do
		local Item = Instance.new("BoolValue")
		Item.Name = v["Name"]
		Item.Value = v["Value"]
		Item.Parent = Inventory
		
		Item.Changed:Connect(function()
			PlayerTable[i]["Value"] = Item.Value
			MaskData:Set(PlayerTable)
			print(PlayerTable)
		end)
	end
end)