DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests

Hey guys, I am working on my Data Store system for my game and it has a lot of values to save so I am using tables to be me efficient. I am getting the warning “DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests,” even though I thought I was only saving for the player one time. Could someone take a look and see where I may be able to make this process more efficient and fix this warning? Thank yoU!

-- Set up table to return to any script that requires this module
local PlayerStatManager = {}


local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData3")

local Leveldata = {
	["Rank"] = 1,
	["XP"] = 0,
	["NeededXP"] = 120,
	["SkillPoints"] = 0,

}

local skills = {
	["HealthSkill_1"] = false,
	["HealthSkill_2"] = false,
	["HealthSkill_3"] = false,

	["EnduranceSkill_1"] = false,
	["EnduranceSkill_2"] = false,
	["EnduranceSkill_3"] = false,

	["AmmoSkill_1"] = false,
	["AmmoSkill_2"] = false,
	["AmmoSKill_3"] = false
}

local survivor = {
	["Survivor"] = "DefaultMale"

}

local survivorsUnlocked = {
	["DefaultMale"] = true,
	["DefaultFemale"] = true,
	["PlagueDoctor"] = true,
	["Horseman"] = true,
	["Channy97"] = true

}

-- Function to add player to the "sessionData" table
local function setupPlayerData(player)

	print("Hi")
	local DataFromStore = nil

	local playerUserId = "Player_" .. player.UserId

	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"

	local dataFolder = player:WaitForChild("leaderstats")

	local Cash = Instance.new("IntValue",dataFolder)
	Cash.Name = "Cash"
	Cash.Value = 0

	local zombieKills = Instance.new("IntValue",dataFolder)
	zombieKills.Name = "ZombieKills"
	zombieKills.Value = 0

	local headshots = Instance.new("IntValue", dataFolder)
	headshots.Name = "Headshots"
	headshots.Value = 0

	for name, value in pairs(Leveldata) do
		local new = Instance.new("NumberValue")
		new.Name = name
		new.Value = value
		new.Parent = leaderstats
	end

	local Skills = Instance.new("Folder",leaderstats)
	Skills.Name = "Skills"

	for name, value in pairs(skills) do
		local new = Instance.new("BoolValue")
		new.Name = name
		new.Value = value
		new.Parent = Skills

	end

	for name, value in pairs(survivor) do
		local new = Instance.new("StringValue")
		new.Name = name
		new.Value = value
		new.Parent = leaderstats

	end

	local SurvivorsUnlocked = Instance.new("Folder", leaderstats)
	SurvivorsUnlocked.Name = "SurvivorsUnlocked"

	for name, value in pairs(survivorsUnlocked) do
		local new = Instance.new("BoolValue")
		new.Name = name
		new.Value = value
		new.Parent = SurvivorsUnlocked

	end

	local XP = leaderstats:WaitForChild("XP")
	local NeededXP = leaderstats:WaitForChild("NeededXP")
	local Rank = leaderstats:WaitForChild("Rank")
	local SkillPoints = leaderstats:WaitForChild("SkillPoints")

	-- LEVEL UP --
	XP.Changed:Connect(function()
		local leaderstats = Instance.new("Folder",player)
		leaderstats.Name = "leaderstats"
		if dataFolder:WaitForChild("XP").Value >= dataFolder:WaitForChild("NeededXP").Value then
			Rank.Value = Rank.Value + 1
			if Rank.Value % 5 == 0 then
				SkillPoints.Value = SkillPoints.Value + 1
			end
			XP.Value = XP.Value - NeededXP.Value
			NeededXP.Value = math.floor(NeededXP.Value * 1.33)
		end
	end)

	local s, e = pcall(function()
		DataFromStore = playerData:GetAsync("Player_" .. player.UserId)
	end)

	local s, e = pcall(function()
		DataFromStore = playerData:GetAsync("Player_" .. player.UserId)
	end)

	if s then
		print("Getting Data For: " .. player.Name)
		if DataFromStore then
			for name, value in pairs(DataFromStore) do
				if player.leaderstats:FindFirstChild(name) then
					print(player.leaderstats[name])
					player.leaderstats[name].Value = value

				elseif player.leaderstats.Skills:FindFirstChild(name) then
					print(player.leaderstats.Skills[name])
					player.leaderstats.Skills[name].Value = value

				elseif player.leaderstats.SurvivorsUnlocked:FindFirstChild(name) then
					print(player.leaderstats.SurvivorsUnlocked[name])
					player.leaderstats.SurvivorsUnlocked[name].Value = value

				else
					print("Not Found")
				end

			end
			print("Data Loaded For: " .. player.Name)
		else
			print("Created New Data For: " .. player.Name)
		end
	else 
		warn("Error Getting Data For: " .. player.Name)
	end
end


local function savePlayerData(player,playerUserId)

	local DataForStore = {}

	for name, value in pairs(player.leaderstats:GetChildren()) do
		if value.Name == "Skills" then
			for name1, value1 in pairs(player.leaderstats.Skills:GetChildren()) do
				DataForStore[value1.Name] = value1.Value
			end
		elseif value.Name == "SurvivorsUnlocked" then
			for name2, value2 in pairs(player.leaderstats.SurvivorsUnlocked:GetChildren()) do
				DataForStore[value2.Name] = value2.Value
			end
		else
			DataForStore[value.Name] = value.Value
		end
	end

	local success = pcall(playerData.SetAsync, playerData, "Player_" .. player.UserId, DataForStore)

	if success then
		print("Successfully Saved Data For: " .. player.Name)
	end

	playerData:SetAsync("Player_" .. player.UserId, DataForStore)

end



-- Function to save player data on exit
local function saveOnExit(player)
	local playerUserId = "Player_" .. player.UserId
	savePlayerData(player,playerUserId)
end

-- Connect "setupPlayerData()" function to "PlayerAdded" event
game.Players.PlayerAdded:Connect(setupPlayerData)

-- Connect "saveOnExit()" function to "PlayerRemoving" event
game.Players.PlayerRemoving:Connect(saveOnExit)

Without really looking at your script … I know that will go off over as little as two wrights. Not really a bad thing it’s just the way it works. Every call after that in a row will add to that queue time also. But they are as good as saved, just waiting in a queue. It’s the ones that don’t make it into that queue that may be lost. You have I think I read 6 seconds total. A lot of time to a computer. That said it’s all to easy to try and save huge amounts of data for every player that ever entered the game. The trick would be to do what you can to minimize that as much as possible. Making everything a data item isn’t a great ideal … Looks like you really don’t have that many but, consider this … that one stat XP you have saved could actually give you the others after you read the XP only. Thinking like that is how you keep that number low.

I’m not sure what you mean by, " that one stat XP you have saved could actually give you the others after you read the XP only." Do you have a reference that I could look at to maybe correlate to your statement?

You can figure all the rest of them off that one stat possibly … so all you really need is that one or two. Just saying you can try to minimize the count that way.

Thank you, but actually the XP resets back to 0 once you are leveled up. Each of these stats are needed individually because some of them are used and changed throughout the game processes independent of eachother.

I’m sure you know what you need to save and I’m sure you understand what I’m saying … gl

Found my issue. Here I was saving the data twice:

local success = pcall(playerData.SetAsync, playerData, "Player_" .. player.UserId, DataForStore)

if success then
	print("Successfully Saved Data For: " .. player.Name)
end

playerData:SetAsync("Player_" .. player.UserId, DataForStore)