Datastore request was added to queue

Hello. This is the second time in having issues with my datastore today. As the title explains, I get an error, saying that Datastore request was added to queue. I have no idea how to add a wait

local DataStore = game:GetService("DataStoreService")
local FlowerManger = DataStore:GetDataStore("flrs")
local ResetManager = DataStore:GetDataStore("resets")

local PlyrServ = game:GetService("Players")

game.Players.PlayerAdded:Connect(function(player)
	local ls = Instance.new("Folder")
	ls.Name = "leaderstats"
	ls.Parent = player
	local flrs = Instance.new("IntValue")
	flrs.Name = "Flowers"
	flrs.Value = 0
	flrs.Parent = ls
	local resets = Instance.new("IntValue")
	resets.Name = "Resets"
	resets.Value = 0
	resets.Parent = ls
	local data
	local reset
	local success,errormessage = pcall(function()
		data = FlowerManger:GetAsync(player.UserId.."-Flowers")
	end)
	if success then
		flrs.Value = data
		print("Loaded In Data")
	else 
		warn(errormessage)
		print("Failed To Load Data")
	end
	local success3, errormessage3 = pcall(function()
	reset = ResetManager:GetAsync(player.UserId.."-Resets")	
	end)
	if success3 then
		resets.Value = reset
		print("Resets have been loaded in")
	else
		warn(errormessage3)
		print("Resets have failed to load in")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success,errormessage = pcall(function()
	FlowerManger:SetAsync(player.UserId.."-Flowers",player.leaderstats.Flowers.Value)
	end)
if success then
	print("Data Success")
else 
	print("Data Failed")
	warn(errormessage)
	end
	
	local success1, errormessage2 = pcall(function()
		ResetManager:SetAsync(player.UserId.."-Resets",player.leaderstats.Resets.Value)
	end)
	if success1 then
		print("Reset Loaded Sucessfully")
	else
		print("Reset Failed To Load")
		warn(errormessage2)
	end
end)

Can you send a picture of the error?

image

The error seems to be coming from a different script as requests.Key in the error is not the same as any of the keys in this script, the keys in this script end in either -Flowers or -Resets.

You can read more on why this warning occurs and what the best practices are to prevent it here: Data Stores

Firstly, the link that you gave me has nothing explaining this error ( at least what I found ) Secondly, I dont have a script that has “requests” in it. Although, there are a lot of scripts that add to the values of -Resets and -Flowers. Thirdly, I still have no idea what to do with my script.

I don’t think it’s an issue if it happens only once. It basically says that your datastore has been queued to be updated, and it might take a while.

It’s weird it happens to error like this when the SetAsync is only connected when the player leaves. Maybe check if the same key is used anywhere else?

1 Like

When you say " a while" how long is " a while"?
Btw, I am adding to Resets and Flowers in workspace. The scripts are in meshes or parts. (Parts being “resets”, “Flowers” being meshes)

By “a while” I mean like 10 seconds. I don’t quite understand how the flowers data works. Do you have a GetAsync for every single flower in your game? Otherwise I wouldn’t see why it should error.

There is no Get:Async.

script.Parent.Touched:Connect(function(hit)
	local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if player then
		player.leaderstats.Flowers.Value = player.leaderstats.Flowers.Value + 1 
		script.Parent.Transparency = 1
		script.Parent.CanCollide = false
		script.Parent.SFX.Playing = true
		script.Disabled = true
		wait(5)
		script.Parent.Transparency = 0
		script.Parent.CanCollide = true
		script.Parent.SFX.Playing = true
		script.Disabled = false
	end
end)

There are also over 1000 flowers in my game, is that the cause?

Data should only be saved or got once. Does your datastore update every time the leaderstats update?

it updates anytime the leaderstats change

That’s what causes the queue. You should only save it at .OnPlayerRemoving() event (once).

So how would I do that? Do I put something inside the PlayerRemoving function?

First off, remove the part where the data saves every time the leaderstats values update.

Instead of that, make something like

game:GetService('Players').PlayerRemoving:Connect(function(player)
      local SaveSuccess, SaveError = pcall(function()
               YourDataStore:SetAsync(player.UserId.."YourKey", your_data_table)
      end)
end)

or

local players = game:GetService("Players")

players.PlayerRemoving:Connect(function(plr)
	local leaderstats = plr:WaitForChild("leaderstats")
	local cash = leaderstats:WaitForChild("Cash")
	repeat 
		local success, errormessage = pcall(function()
			DataStore:SetAsync(plr.UserId, cash.Value)
		end)
	until success 
end)

The error still occurs

local DataStore = game:GetService("DataStoreService")
local FlowerManger = DataStore:GetDataStore("flrs")
local ResetManager = DataStore:GetDataStore("resets")
local PlyrServ = game:GetService("Players")

game.Players.PlayerAdded:Connect(function(player)
	local ls = Instance.new("Folder")
	ls.Name = "leaderstats"
	ls.Parent = player
	local flrs = Instance.new("IntValue")
	flrs.Name = "Flowers"
	flrs.Value = 0
	flrs.Parent = ls
	local resets = Instance.new("IntValue")
	resets.Name = "Resets"
	resets.Value = 0
	resets.Parent = ls
	local data
	local reset
	local success,errormessage = pcall(function()
		data = FlowerManger:GetAsync(player.UserId.."-Flowers")
	end)
	if success then
		flrs.Value = data
		print("Loaded In Data")
	else 
		warn(errormessage)
		print("Failed To Load Data")
	end
	local success3, errormessage3 = pcall(function()
	reset = ResetManager:GetAsync(player.UserId.."-Resets")	
	end)
	if success3 then
		resets.Value = reset
		print("Resets have been loaded in")
	else
		warn(errormessage3)
		print("Resets have failed to load in")
	end
end)

PlyrServ.PlayerRemoving:Connect(function(plr)
		local leaderstats = plr:WaitForChild("leaderstats")
	local cash = leaderstats:WaitForChild("Flowers")
	local resetva = leaderstats:WaitForChild("Resets")
		repeat 
			local success, errormessage = pcall(function()
			FlowerManger:SetAsync(plr.UserId, cash.Value)
			ResetManager:SetAsync(plr.UserId, resetva.Value)
			end)
	until success 
end)

Ok, maybe remove the repeat loop:

local DataStore = game:GetService("DataStoreService")
local FlowerManger = DataStore:GetDataStore("flrs")
local ResetManager = DataStore:GetDataStore("resets")
local PlyrServ = game:GetService("Players")

game.Players.PlayerAdded:Connect(function(player)
	local ls = Instance.new("Folder")
	ls.Name = "leaderstats"
	ls.Parent = player
	local flrs = Instance.new("IntValue")
	flrs.Name = "Flowers"
	flrs.Value = 0
	flrs.Parent = ls
	local resets = Instance.new("IntValue")
	resets.Name = "Resets"
	resets.Value = 0
	resets.Parent = ls
	local data
	local reset
	local success,errormessage = pcall(function()
		data = FlowerManger:GetAsync(player.UserId.."-Flowers")
	end)
	if success then
		flrs.Value = data
		print("Loaded In Data")
	else 
		warn(errormessage)
		print("Failed To Load Data")
	end
	local success3, errormessage3 = pcall(function()
	reset = ResetManager:GetAsync(player.UserId.."-Resets")	
	end)
	if success3 then
		resets.Value = reset
		print("Resets have been loaded in")
	else
		warn(errormessage3)
		print("Resets have failed to load in")
	end
end)

PlyrServ.PlayerRemoving:Connect(function(plr)
		local leaderstats = plr:WaitForChild("leaderstats")
	local cash = leaderstats:WaitForChild("Flowers")
	local resetva = leaderstats:WaitForChild("Resets")

			local success, errormessage = pcall(function()
			FlowerManger:SetAsync(plr.UserId, cash.Value)
			ResetManager:SetAsync(plr.UserId, resetva.Value)
			end)

end)

Removing the loop does not fix the problem

There is no reason why it would queue if it’s only this one script that handles datastores.

datastore has limits and if you try to use the datastore to often when you have consumed your limits it will be added to a queue

you can read about datastore limits here: DataStore Limits

so as we have limits its better to put all are player data in one single key so we can load and write the player data in one single call

if you want to know how to write all the player data in one key here is a video that might help