Datastore request was added to queue

so i made a datastore to save material

local DSS = game:GetService("DataStoreService")
local materialDS = DSS:GetDataStore("PlayerMaterials")

game.Players.PlayerAdded:Connect(function(player)
	local materialFolder = Instance.new("Folder",player)
	materialFolder.Name = "materialFolder"
	
	local wood = Instance.new("IntValue",materialFolder)
	wood.Value = 0
	wood.Name = "wood"
	
	local iron = Instance.new("IntValue",materialFolder)
	iron.Value = 0
	iron.Name = "iron"
	
	local rock = Instance.new("IntValue",materialFolder)
	rock.Value = 0
	rock.Name = "rock"
	
	local steel = Instance.new("IntValue",materialFolder)
	steel.Value = 0
	steel.Name = "steel"
	
	local playerId = player.userId
	
	local suc,err
	repeat
		suc,err = pcall(function()
			return materialDS:GetAsync(playerId)
		end)
		wait()
	until suc
	wood.Value = err and err["Wood"] or 0
	steel.Value = err and err["Canes"] or 0
	rock.Value = err and err["Rock"] or 0
	iron.Value = err and err["Iron"] or 0
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerId = player.userId
	local statsTable = {
		["Wood"]=player.materialFolder.wood.Value,
		["Steel"]=player.materialFolder.steel.Value,
		["Rock"] = player.materialFolder.rock.Value,
		["Iron"]=player.materialFolder.steel.Value
	}
	
	local success,err = pcall(function()
		materialDS:SetAsync(playerId,statsTable)
	end)
	
	if success then
		print("data")
	else 
		print("failed")

	end
	
end)

game:BindToClose(function()
	local players = game.Players:GetPlayers()
	for i,player in pairs(players) do
		local playerId = player.userId
		local statsTable = {
			["Wood"]=player.materialFolder.wood.Value,
			["Steel"]=player.materialFolder.steel.Value,
			["Rock"] = player.materialFolder.rock.Value,
			["Iron"]=player.materialFolder.steel.Value
		}
		local success,err = pcall(function()
			materialDS:SetAsync(playerId,statsTable)
		end)

		if success then
			print("data saved")
		else 
			print("failed")

		end
	end	
end)

but this warnings comes up:
DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 247406010

1 Like

Hello,
I’m not sure if it changes really anything, but in your script everytime you want to get a User ID from a Player Instance, you are using player.userId with lowercase u, I think you should be using player.UserId with uppercase U. If this player.userId is a nil then your pcall will keep returning error because you are trying to use nil as a key. That will lead in your script just infinitly trying to do the pcall and that will lead into the warning that you are making too much requests. I might be wrong, but this is the thing that I’m thinking is causing this problem.

3 Likes

Player.userId is the same as Player.UserId, but the former is deprecated.

2 Likes

I changed it but its still the same warning

Here are some other threads that provide solutions to this:

Additional resources can be found on the Roblox Developer Hub

Data Store Errors and Limits

Thanks , i will check them out

1 Like

Hey man, now when i test it by playing the game it saves properly. Before i was testing it in studio. Am i supposed to test datastores in game? it saves and loads correctly too but the warning still exists in studio

I believe you can test it in either environment – if you’re playing the game from the website, you can also open up the Developer Console by clicking F9 and swapping to the Server view to see the Output there, if needed.

From my understanding, so long as the error is not excessive (such as upwards of a dozen every few seconds, around the mark that loleris, a prominent developer within the community, mentioned here), I don’t believe it’s a massive issue.

I’m not sure what changes you made to your code after reading through the other threads, so I would suggest testing it in an environment with more individuals/testing in Studio with multiple player instances via the “Test” tab, to see if the adjustments provided sufficient optimizations.

i mean it works and it saves all data is just warning me like this again
DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 247406010

it saves data when i normally play the game but theres no way i can see if its warning me there since its when i leave that the warning appears

Read the warning. You are calling GetAsync too much and you are overloading the Data request limit.

isnt it SetAsync? and im only calling it 2 times, when the server shutdowns and when the player leaves

You are calling GetAsync in a repeat loop for whatever reason, if data is nil then this will repeat forever.

Is “API access in studio” (or whatever its called) enabled? If its not then success will never become true. I also suggest to add a max tries (after n failed tries just give up) and logging the error message

i changed it to this

local data 
	local success, err = pcall(function()

		data = materialDS:GetAsync(player.UserId)
	end)

	if success then 

		wood.Value = data[1] 
		steel.Value = data[2] 
		rock.Value = data[3]
		iron.Value = data[4]

	else 
		print("The player has no data!") 
	end

same warning , and the warning occurs when i leave so doesnt it have to do with saving?

Yes, API access is enabled in studio

Never mind, I read your code wrong. Success won’t return false if the player has no data, but you still shouldn’t be using a repeat loop.

I believe that your data is saving twice at the same time (on BindToClose and on PlayerRemoving) as you are the only person in the game.

1 Like

i mean its just a warning but the data saves, so i shouldnt be worried?

I really suggest that you change your code in order for this warning to not appear.

If there are too many requests in queue, some may be ignored and thus not saving/updating the data.

so i tested it with 2 more people, the warning occurs only when the server shutdowns or the last player in the server leaves which means it shutdowns

i fixed it by checking if when the player leaves the server has more than 1 player in it. Works perfectly now