Is it possible to get every async saved under a datastore?

Basically I need to loop through every async saved in a datastore, I could use ordered asyncs but last time I checked thats not how they work, as they are different and the value you’d be saving is required to be a number.

So what can I do instead?

uhhh more context please?

like are you trying to display every async/data somewhere??

i am confu sed

do you mean every entry? you can use DataStore:ListKeysAsync()

also async isnt an entry its actually short for the word asynchronous and it’s used in data store operations because each method is added to code that is run asynchronously

asynchronous code is code run at the same time as another piece of code

Following this; I believe ListKeysAsync will only return up to 512 keys, so if you have more than that you’ll have to use Pages/Cursor to iterate through it.

Ahhh I see ok, Ill check that out.

Oh ok, I didnt know what to call it so I went with that lol

Something along the lines of that

How would I go about doing that?

I think @IAmTailerr was referring to the page size of :ListKeysAsync(), which returns a DataStoreKeyPages object. You need to iterate over all of the pages to get all of the keys.

local function listKeys()
    --you can add other parameters to this like a custom cursor for the iteration
    local success, keys = pcall(DataStore.ListKeysAsync, DataStore)

    if not success then
        warn(result)
        return nil
    end

    while not pages.IsFinished do
        --get current page
        local page = keys:GetCurrentPage()

        --iterate over the current page
        for _, key in next, page, nil do
            print(key) --outputs the key data is associated with
        end
        
        --advance to the next page
        keys:AdvanceToNextPageAsync()
    end
end
local DataStoreService = game:GetService("DataStoreService")
local Ds = DataStoreService:GetDataStore("idkDataStore")

local function getAllData()
    local dataDictionary = {}
    local success, pages = pcall(function()
        return Ds:ListKeysAsync()
    end)

    if success then
        while true do
            local items = pages:GetCurrentPage()
            for _, item in ipairs(items) do
                local key = item.KeyName
                local value = Ds:GetAsync(key)
                dataDictionary[key] = value
            end
            if pages.IsFinished then
                break
            end
            pages:AdvanceToNextPageAsync()
        end
    else
        warn("Failed to list keys")
    end

    return dataDictionary
end

local allData = getAllData()
for key, value in pairs(allData) do
    print(key, value)
end

This will allow you to loop through all entries in your datastore without needing to use ordered datastores

1 Like