How to prevent :RemoveAsync from throttling?

How much time should I wait to prevent :RemoveAsync from throttling?

You can monitor RemoveAsync’s budget using DataStoreService’s GetRequestBudgetForRequestType method, like so:

local DataStoreService = game:GetService("DataStoreService")

local dataStore = DataStoreService:GetDataStore("Example")

local function waitForDataStoreBudget()
	local requestBudget = DataStoreService:GetRequestBudgetForRequestType(Enum.DataStoreRequestType.SetIncrementAsync)
	if requestBudget > 0 then return end

	task.wait(0.1)
	waitForDataStoreBudget()
end

local success, dataStoreKeyPages = pcall(dataStore.ListKeysAsync, dataStore)

if success then
	while true do
		for _, dataStoreKey in dataStoreKeyPages:GetCurrentPage() do
			waitForDataStoreBudget()

			local success, error = pcall(dataStore.RemoveAsync, dataStore, dataStoreKey.KeyName)
			if success then continue end

			warn("Failed to remove data for key: " .. dataStore.KeyName)
		end

		if dataStoreKeyPages.IsFinished then
			print("Keys removed successfully")
			break
		else
			dataStoreKeyPages:AdvanceToNextPageAsync()
		end
	end
else
	warn("Failed to get DataStoreKeyPages. Error: " .. dataStoreKeyPages)
end

The main focus of the script above is the waitForDataStoreBudget function. It works by retrieving the budget left for RemoveAsync (which falls under Enum.DataStoreRequestType.SetIncrementAsync, according to the docs), and prevents the for loop that’s iterating over the DataStore’s keys from continuing to run, by yielding using the task.wait function, until the budget is greater-than 0, which will prevent throttling from occuring

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.