Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

DataStore request was added to queue. If request queue fills, further Try sending fewer requests??

Hi I keep receiving this error on my House Data Store system: DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key. Script:

local DataStoreService = game:GetService("DataStoreService")

local MainData = DataStoreService:GetDataStore("MainData")

local HouseData = DataStoreService:GetDataStore("HouseData")

--Variables--

function saveHouseData(player)



	local Houses = {}

	for i, value in pairs(player.Houses:GetChildren()) do

	

		Houses[value.Name] = value.Value

	

	end

	HouseData:SetAsync(player.UserId, Houses)

end

--House Data--

game.Players.PlayerAdded:Connect(function(player)



	local Plot = Instance.new("StringValue")

	Plot.Name = "Plot"

	Plot.Value = ""

	Plot.Parent = player

	

	local Houses = Instance.new("Folder")

	Houses.Name = "Houses"

	Houses.Parent = player

	

	for i, house in pairs(game.ReplicatedStorage.Houses:GetChildren()) do

		local newValue = Instance.new("BoolValue")

		newValue.Name = house.Name

		newValue.Value = false

		newValue.Parent = Houses

		

	end

	local houseData = HouseData:GetAsync(player.UserId) or {}

	

	for houseName, houseValue in pairs(HouseData) 

		Houses[houseName].Value = houseValue

	end

end)





--House Saving--

game.Players.PlayerRemoving:Connect(function(player)

	if game.Workspace:FindFirstChild(player.Name.."'sHouse") then

		game.Workspace:FindFirstChild(player.Name.."'sHouse"):Destroy()

	end	

	local PlayerPlot = player.Plot

	

	game.Workspace:FindFirstChild(PlayerPlot.Value).CurrentOwner.Value = ""

	player.Plot.Value = ""

	end)

	

game.Players.PlayerRemoving:Connect(saveHouseData)

game:BindToClose(function()

	for i, player in pairs(game.Players:GetPlayers()) do

		saveHouseData(player)

		end

end)

Updated

I got rid of all errors datastore still doesn't work here is the updated code

local DataStoreService = game:GetService("DataStoreService")

local MainData = DataStoreService:GetDataStore("MainData")

local HouseData = DataStoreService:GetDataStore("HouseData")



local Players = game:GetService("Players")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local RunService = game:GetService("RunService")



local function waitForRequestBudget(requestType: Enum.DataStoreRequestType)

	local currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)

	while currentBudget < 1 do

		currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)

		task.wait(5)

	end

end



-- House Data --

game.Players.PlayerAdded:Connect(function(player)



local Plot = Instance.new("StringValue")

Plot.Name = "Plot"

Plot.Value = ""

	Plot.Parent = player

	

	local Houses = Instance.new("Folder")

	Houses.Name = "Houses"

	Houses.Parent = player

	for i, house in pairs(ReplicatedStorage.Houses:GetChildren()) do

		local newValue = Instance.new("BoolValue")

		newValue.Name = house.Name

		newValue.Value = false

		newValue.Parent = Houses

		

	end

end)



function loadHouseData(player)



	local Houses = Players.Houses





	waitForRequestBudget(Enum.DataStoreRequestType.GetAsync)

	local success, houseData = pcall(HouseData.GetAsync, HouseData, player.UserId)

	if success then

		for houseName, houseValue in pairs(HouseData) do

			Houses[houseName].Value = houseValue

	end

	else -- if it errors (mostly when player has no data)

	print("Player " .. player.Name .. " has no data!")

	warn(houseData) -- warns the error message

end

end



-- House Saving --

function saveHouseData(player)

	local Houses = {}

	local success

	repeat

		waitForRequestBudget(Enum.DataStoreRequestType.UpdateAsync)

		success = pcall(HouseData.UpdateAsync, HouseData, player.UserId, function()

			return Houses		

		end)

	until success

	if workspace:FindFirstChild(player.Name.."'sHouse") then

		game.Workspace:FindFirstChild(player.Name.."'sHouse"):Destroy()

	end

	local Plot = player.Plot

	local PlayerPlot = player.Plot

	workspace:FindFirstChild(Plot)player.Plot.Value = ""

	player.Plot.Value = ""



	local Houses = {}



	for i, value in pairs(player.Houses:GetChildren()) do

		Houses[value.Name] = value.Value

	end

end

for _, player in ipairs(Players:GetPlayers()) do

	coroutine.wrap(loadHouseData)(player)

end



game.Players.PlayerAdded:Connect(function(loadHouseData)

	game.Players.PlayerRemoving:Connect(saveHouseData)



	game:BindToClose(function()

		if RunService:IsStudio() then

			task.wait(2)

			print("task waited")

		else

		

			local finished = Instance.new("BindableEvent")

	

			local allPlayers = Players:GetPlayers()



			local leftPlayers = #allPlayers

			for _, player in ipairs(allPlayers) do

				coroutine.wrap(function()

					saveHouseData(player)

					leftPlayers -= 1

					if leftPlayers == 0 then

						finished:Fire()

						print("finishing")

					end

				end)()

			end



			finished.Event:Wait()

end

		end)

	end)

1 comment

0

Sorry about the spacing I sent the code to my other computer to post it and the format gets weird

1 answer

✓ Accepted answer

Solution

This is where RequestBudget comes in. If there isn't enough RequestBudget, it will show that warning. To fix that, you will need to wait until there is enough budget.

local function waitForRequestBudget(requestType: Enum.DataStoreRequestType)
	local currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
	while currentBudget < 1 do
		currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
		task.wait(5)
	end
end

Now you can use that function in GetAsync() and SetAsync().

-- House Data --
waitForRequestBudget(Enum.DataStoreRequestType.GetAsync)
local houseData = HouseData:GetAsync(player.UserId) or {}

-- House Saving --
waitForRequestBudget(Enum.DataStoreRequestType.SetAsync)
HouseData:SetAsync(player.UserId, Houses)

Pcall (Protected Call)

I recommend using a pcall() when using an asynchronous function since they can error and break the script. pcall() calls a function and if it errors, it won't affect the script and the error message will just get returned by pcall(). If it didn't error and it was successful, pcall() will return everything returned by the called function.

-- House Data --
waitForRequestBudget(Enum.DataStoreRequestType.GetAsync)

local success, houseData = pcall(function()
	return HouseData:GetAsync(player.UserId)
end)
-- or
local success, houseData = pcall(HouseData.GetAsync, HouseData, player.UserId)

if success == true then -- if successful
	for houseName, houseValue in pairs(HouseData) 
		Houses[houseName].Value = houseValue
	end
else -- if it errors (mostly when player has no data)
	print("Player " .. Player.Name .. " has no data!")
	warn(houseData) -- warns the error message
end

Retries

If you use SetAsync() once, there's a chance it might error, not saving the data. What you can do is retry until data is saved.

-- House Saving --
local success
repeat
	waitForRequestBudget(Enum.DataStoreRequestType.SetAsync)
	success = pcall(HouseData.SetAsync, HouseData player.UserId, Houses)
until success == true

UpdateAsync > SetAsync

UpdateAsync() is safer to use in multi-server attempts than SetAsync(), so you should use it except when using an OrderedDataStore (commonly used in global leaderboards).

-- House Saving --
local success
repeat
	waitForRequestBudget(Enum.DataStoreRequestType.UpdateAsync)
	success = pcall(HouseData.UpdateAsync, HouseData, player.UserId, function()
		return Houses
	end)
until success == true

Final Script

local DataStoreService = game:GetService("DataStoreService")
local MainData = DataStoreService:GetDataStore("MainData")
local HouseData = DataStoreService:GetDataStore("HouseData")

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

local function waitForRequestBudget(requestType: Enum.DataStoreRequestType)
	local currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
	while currentBudget < 1 do
		currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
		task.wait(5)
	end
end

-- House Data --
function loadHouseData(player: Player?)
	local Plot = Instance.new("StringValue")
	Plot.Name = "Plot"
	Plot.Value = ""
	Plot.Parent = player
	
	local Houses = Instance.new("Folder")
	Houses.Name = "Houses"
	Houses.Parent = player
	
	for i, house in pairs(ReplicatedStorage.Houses:GetChildren()) do
		local newValue = Instance.new("BoolValue")
		newValue.Name = house.Name
		newValue.Value = false
		newValue.Parent = Houses
	end
	
	waitForRequestBudget(Enum.DataStoreRequestType.GetAsync)
	local success, houseData
	local retries = 0
	repeat
		waitForRequestBudget(Enum.DataStoreRequestType.GetAsync)
		success, houseData = pcall(HouseData.GetAsync, HouseData, player.UserId)
		retries += 1
	until (success and houseData) or retries >= 5
	
	if success then
		for houseName, houseValue in pairs(houseData) do
			Houses[houseName].Value = houseValue
		end
	else -- if it errors (mostly when player has no data)
		print("Player " .. Player.Name .. " has no data!")
		warn(houseData) -- warns the error message
	end
end

-- House Saving --
function saveHouseData(player: Player, dontWait: boolean?)
	if workspace:FindFirstChild(player.Name.."'sHouse") then
		workspace:FindFirstChild(player.Name.."'sHouse"):Destroy()
	end
	
	local PlayerPlot = player.Plot
	workspace:FindFirstChild(PlayerPlot.Value).CurrentOwner.Value = ""
	player.Plot.Value = ""
	
	local Houses = {}
	
	for i, value in pairs(player.Houses:GetChildren()) do
		Houses[value.Name] = value.Value
	end
	
	local success
	repeat
		if not dontWait then
			waitForRequestBudget(Enum.DataStoreRequestType.UpdateAsync)
		end
		success = pcall(HouseData.UpdateAsync, HouseData, player.UserId, function()
			return Houses
		end)
	until success
end

for _, player in ipairs(Players:GetPlayers()) do
	coroutine.wrap(loadHouseData)(player)
end

Players.PlayerAdded:Connect(loadHouseData)
Players.PlayerRemoving:Connect(saveHouseData)

game:BindToClose(function()
	if RunService:IsStudio() then
		task.wait(2)
	else
		local finished = Instance.new("BindableEvent")
		local allPlayers = Players:GetPlayers()
		local leftPlayers = #allPlayers
		
		for _, player in ipairs(allPlayers) do
			coroutine.wrap(function()
				saveHouseData(player, true)
				leftPlayers -= 1
				if leftPlayers == 0 then
					finished:Fire()
				end
			end)()
		end
		
		finished.Event:Wait()
	end
end)

Source

6 comments

0

It still doesn't work. I edited the script a bit to fix somethings. Errors: First error is when a player doesn't have a plot claimed and buys a house and then leaves error code line 123 ServerScriptService.DataManager:121: attempt to index nil with 'CurrentOwner' but if a player does have plot claimed and buys house then leaves no errors come up and won't save data though

0

I updated my post with the new code if you can have look at it -- Thanks

0

I got rid of all the errors but datastore doesn't work still

0

OH

0

The error was on line 76 and I forgot to removed "function(" so I changed my answer and removed it.

0

ok now I keep getting error on line 38 ServerScriptService.DataManager:38: invalid argument #1 to 'pairs' (table expected, got Instance)

This discussion is preserved as it was. New answers, comments, and votes are closed.