DataStore request was added to queue. Help?

When i end a test game in studio, the app lags for a good 10 seconds and then i get this message in the output " DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key". I don’t know what im doing wrong. I also have another script similar just with a few different changes, do i merge the scripts?

local Players = game:GetService("Players")
local DSService = game:GetService("DataStoreService")
local data = DSService:GetDataStore("TableIndex003")
Players.PlayerAdded:Connect(function(player)
	Instance.new("Folder",player).Name = "Index"
	local key = player.UserId
	local Index = player:WaitForChild("Index")
	for i,v in pairs(game.ReplicatedStorage:WaitForChild("Pets"):GetChildren()) do
		Instance.new("BoolValue",Index).Name = tostring(v)
	end
	wait(1)
	local savedLevel = data:GetAsync(key)
	if savedLevel ~= nil then
		for i, data in pairs(savedLevel) do
			local pet = Index:FindFirstChild(data[1])
			if pet then
				pet.Value = data[2]
			else
				local instance = Instance.new(data[3])
				if instance then
					instance.Name = data[1]
					instance.Value = data[2]
					instance.Parent = Index
				end
			end
		end
	else
		data:SetAsync(key, {})
	end
end)
Players.PlayerRemoving:Connect(function(player)
	local key = player.UserId
	local savedLevel = data:GetAsync(key)
	local Index = player:FindFirstChild("Index")
	if not Index then return end
	Index = Index:GetChildren()
	local objData = {}
	for i, obj in pairs(Index) do
		table.insert(objData, {obj.Name, obj.Value, obj.ClassName})
	end
	if savedLevel == nil then
		data:SetAsync(key, objData)
	else
		data:UpdateAsync(key, function(oldValue)
			oldValue = objData
			return objData
		end)
	end
end)
game:BindToClose(function()
	for i, player in pairs(Players:GetPlayers()) do
		local key = player.UserId
		local savedLevel = data:GetAsync(key)
		local Index = player:FindFirstChild("Index")
		if not Index then return end
		Index = Index:GetChildren()
		local objData = {}
		for i, obj in pairs(Index) do
			table.insert(objData, {obj.Name, obj.Value, obj.ClassName})
		end
		if savedLevel == nil then
			data:SetAsync(key, objData)
		else
			data:UpdateAsync(key, function(oldValue)
				oldValue = objData
				return objData
			end)
		end
	end
	wait(3)
end
1 Like

First of all, avoid Instance.new(className, parent) as it is deprecated.

Second, you might wanna consider using a separate function to save player data, because both your BindToClose() and PlayerRemoving() script seems to be pretty much identical.

The reason why you may have that issue (I’m just taking a stab at the dark here) is that when you stop Studio, both BindToClose and PlayerRemoving are fired. If I recall, you can’t overwrite the same data within 6 seconds of each other.

You’re getting that message because you’re sending datastore requests too quickly.

Like @NINJAMASTR999 said, you are probably getting that warning while running in studio. To prevent this, you need to check if the game is running in studio or not in the bindtoclose.

Like this : (Sorry I am on phone)
If not game:GetService(“RunService”):IsStudio() then
(Save each player’s data)
end

I also suggest that you wrap the UpdateAsync and SetASync with pcalls as it can prevent any game breaking errors.