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

I was making leaderstats for my game, and got this warning spammed in the output:

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

Is there any way to fix this? It causes data loss.

leaderstats
local DataStoreService = game:GetService("DataStoreService")
local Store = DataStoreService:GetDataStore("JumpStore")
local Store2 = DataStoreService:GetDataStore("CashStore")

game.Players.PlayerAdded:Connect(function(plr)
	local UserId = plr.UserId
	local Currency = Store:GetAsync(UserId)
	local Currency2 = Store2:GetAsync(UserId)
	
	if Currency == nil then
		Currency = 0
		Store:SetAsync(UserId, Currency)
	end
	
	if Currency2 == nil then
		Currency2 = 0
		Store2:SetAsync(UserId, Currency2)
	end
	
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"
	
	local Stat = Instance.new("IntValue", leaderstats)
	Stat.Name = "Jumps"
	Stat.Value = Currency
	
	local Stat2 = Instance.new("IntValue", leaderstats)
	Stat2.Name = "Cash"
	Stat2.Value = Currency2
	
	Stat2.Changed:Connect(function(NewValue2)
		Store2:SetAsync(UserId, NewValue2)
	end)
	
	Stat.Changed:Connect(function(NewValue)
		Store:SetAsync(UserId, NewValue)
	end)
end)

It looks like you’re saving data every time the stat changes. Generally speaking, this isn’t an efficient way to go about this. In my own work to save data, I usually save data whenever a player leaves the game with PlayerRemoving, as well as hooking it up to a BindToClose function. It would also be smart to store all values in a table of some sorts and save that, instead of using multiple datastores.

If you need any more help let me know! :slight_smile:

7 Likes

I understood about 10% of what you said

2 Likes

Would this be better?

local service = game:GetService("DataStoreService")
local datastore = service:GetDataStore("stat") --Data store name

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"
	
	local stat = Instance.new("IntValue", leaderstats)
	stat.Name = "stat" --Stat name
	
	local data
	local success, errorMessage = pcall(function()
		data = datastore:GetAsync(plr.UserId.."-stat") --change stat to currency name
	end)
	
	if success then
		stat.Value = data
	else
		warn(errorMessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, errorMessage = pcall(function()
		--change both stat to stat name
		datastore:SetAsync(plr.UserId.."-stat", plr.leaderstats.stat.Value) 
	end)
	
	if not success then
		warn(errorMessage)
	end
end)

I dont use it because its complicated (to me)

1 Like

This looks like you’re using pcalls as well as PlayerRemoving, meaning this would be a good alternative which should prevent the error! If you don’t use it because it’s too complicated, allow me to explain some concepts! :smiley:

Loading in data here, you’ll notice it’s wrapped in something called a “pcall”. Pcalls are good, as if something fails within the block, it won’t stop the entire rest of the script from running. This is useful for datastores and stuff, as anything contacting something externally from Roblox has the chance to fail every now and then.

local data
	local success, errorMessage = pcall(function()
		data = datastore:GetAsync(plr.UserId.."-stat") --change stat to currency name
	end)

The block below says that if the data was loaded in, set the value to the data. Else, If it’s not successful, then warn the output of the error message.

	if success then
		stat.Value = data
	else
		warn(errorMessage)
	end
end)

Other than that I think everything else you used in your original script other than PlayerRemoving! It might also be a good idea to add BindToClose in there as well!

3 Likes

What is BindToClose?

(I am new to scripting)

1 Like

No worries. BindToClose is useful if the server shuts down unexpectedly (ie the game crashes or something). Here is a quick sample.

game:BindToClose(function()
	for i, plr in pairs(game.Players:GetPlayers()) do
		--save the players data here, basically what you did in playerRemoving.
	end
end)

2 Likes

Ok, thanks for the help! (characterlimitughhh)

1 Like

Its also a good idea to save data every 5 minutes or so (for extra redundancy)

1 Like