Datastore optimizing

How can I optimize these 2 functions inside my module script?
Currently it takes like 1 minute just for getAllAwaitingResultData function to run. And that is considering there is only 3 keys stored in the datastore, but I plan on having hundreds in the future.

local getAsyncCooldown = 2
local set_remove_AsyncCooldown = 2
local listKeyAsyncCooldown = 10
local advanceToNextPageAsyncCooldown = 10

local lastGetTime = os.time()
local lastSetRemoveTime = os.time()
local lastListTime = os.time()
local lastAdvanceTime = os.time()

function systemManager.getAllAwaitingResultData()
	repeat
		task.wait(0.1)
	until os.time()-lastListTime > listKeyAsyncCooldown
	lastListTime = os.time()
	
	local dataDictionary = {}
	local listSuccess, pages = pcall(function()
		return awaitingResult:ListKeysAsync()
	end)

	if listSuccess then
		while true do
			local items = pages:GetCurrentPage()
			coroutine.wrap(function()
				for _, v in ipairs(items) do
					repeat
						task.wait(0.1)
					until os.time()-lastGetTime > getAsyncCooldown
					lastGetTime = os.time()
					local value = awaitingResult:GetAsync(v.KeyName)
					dataDictionary[v.KeyName] = value
				end
			end)()
			if pages.IsFinished then
				break
			end
			repeat
				task.wait(0.1)
			until os.time()-lastAdvanceTime > advanceToNextPageAsyncCooldown
			lastAdvanceTime = os.time()
			pages:AdvanceToNextPageAsync()
		end
	end
	return dataDictionary
end

function systemManager.getAllAppResultData()
	repeat
		task.wait(0.1)
	until os.time()-lastListTime > listKeyAsyncCooldown
	lastListTime = os.time()
	
	local dataDictionary = {}
	local listSuccess, pages = pcall(function()
		return appResult:ListKeysAsync()
	end)

	if listSuccess then
		while true do
			local items = pages:GetCurrentPage()
			coroutine.wrap(function()
				for _, v in ipairs(items) do
					-- Take current key + its associated value and write it into the dictionary.
					repeat
						task.wait(0.1)
					until os.time()-lastGetTime > getAsyncCooldown
					lastGetTime = os.time()
					local value = appResult:GetAsync(v.KeyName)
					dataDictionary[v.KeyName] = value
				end
			end)()
			if pages.IsFinished then
				break
			end
			repeat
				task.wait(0.1)
			until os.time()-lastAdvanceTime > advanceToNextPageAsyncCooldown
			lastAdvanceTime = os.time()
			pages:AdvanceToNextPageAsync()
		end
	end
	return dataDictionary
end

im sorry if this script is a mess, I have been trying to fix this legit all day ;( please help lol

Uhh try to limit constant running loops

If pages is the result of awaitingResult:ListKeysAsync, and v is iterated over pages:GetCurrentPage through the items variable, can’t you just use v.value instead of another :GetAsync? Because from my understanding, the pages object contains all the data in the datastore, and you’re just redundantly getting it twice but way slower.

1 Like

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