So i have a problem with coroutines. I am making a data management module and it needs coroutines to prevent “clogging”. the coroutines are wrapping all of the code that can yield.
local threadSuccess, getSuccess, response = coroutine.wrap(function()
self.gettingStores[key] = true
local orderedDataStore: OrderedDataStore = DSS:GetOrderedDataStore(key.."/"..self.name)
local globalDataStore: GlobalDataStore = DSS:GetDataStore(key.."/"..self.name)
local attempts: number = 0
local data: string = nil
local success: boolean = false
local response: data | errorType = nil
local backup: boolean = false
local versionGotten: number = 0
local errorLog: {[string]: number} = {}
local orderedSuccess: boolean, response: DataStorePages | errorType = getOrderedData(orderedDataStore)
if not orderedSuccess then return false, response :: errorType end
local ordered: DataStorePages = response :: DataStorePages
local page = ordered:GetCurrentPage()
repeat
for version, info in page do
success, response = getData(globalDataStore, info.value)
if response ~= nil then
data = response :: string
versionGotten = info.value
else
backup = true
end
attempts += 1
end
if data ~= nil or attempts >= 5 or ordered.IsFinished then
break
end
local success, errorLog = advancePage(ordered)
if not success then
return false, errorLog :: errorType
end
page = ordered:GetCurrentPage()
until data or attempts >= 5
local returnedData: data? = decompressData(data)
if not returnedData then
if not returnedData then
if default then
return true, default
else
return false, "no data found"
end
end
else
returnedData.currentVersion = versionGotten
self.cache[key] = returnedData
self.gettingStores[key] = false
return true, returnedData
end
return false, "an unknown error occured\n"..debug.traceback().."\n please report any incidents of this to the developers"
end)()
the function above is what i use to fetch data. when the function getOrderedData on line 16 after it returns the values. I have no idea why, i have asked AI, I have tried different implementations of coroutines, I have done all of it.
I would really like to keep it in a separate function, but if i have to i can move the code from it into that block.
I just seriously need help with why the code runs perfectly fine until then.