Problem with AdvanceToNextPageAsync() return nil

Hi everyone,

I’m currently working on an in-game catalog for my Roblox game and I’ve encountered an issue with the AdvanceToNextPageAsync function. When I call this function, it returns nil, even though GetCurrentPage and IsFinished work just fine.

Here is a snippet of my script:

local replicatedStorage = game:GetService("ReplicatedStorage")
local AvatarEditorService = game:GetService("AvatarEditorService")
local Promise = require(replicatedStorage.Packages.Promise)
local Categories = require(script.Parent.Parent.Constant.Categories)
local FetchedCatalogIds = require(script.Parent.Parent.Actions.FetchedCatalogIds)
local CatalogPage = require(script.Parent.Parent.Actions.CatalogPage)

local catalogSearchParams = function(categoryIndex, subCategoryIndex)
    local categoryDetails = Categories[categoryIndex]
    local searchParams = CatalogSearchParams.new()

    if categoryDetails.Subcategories then
        local subCategoryDetails = categoryDetails.Subcategories[subCategoryIndex]
        if subCategoryDetails.CategoryFilter then
            searchParams.CategoryFilter = subCategoryDetails.CategoryFilter
        end
        if subCategoryDetails.AssetTypes then
            searchParams.AssetTypes = subCategoryDetails.AssetTypes
        end
    end

    if categoryDetails.BundleTypes then
        searchParams.BundleTypes = categoryDetails.BundleTypes
    end
    searchParams.SortType = Enum.CatalogSortType.Relevance
    return searchParams
end

return function (categoryIndex, subCategoryIndex, nextPageCursor)
    return function (store)
        return Promise.new(function(resolve, reject)
            local items = {}
            coroutine.wrap(function()
                local objectPage
                local currentPage

                local params = catalogSearchParams(categoryIndex, subCategoryIndex)

                if nextPageCursor then
                    objectPage = nextPageCursor
                    if objectPage and not objectPage.IsFinished then
                        print("Type of objectPage: ", typeof(objectPage))
                        local success, result = pcall(function()
                            return objectPage:AdvanceToNextPageAsync()
                        end)
                        if success then
                            currentPage = result
                            print("Initialized currentPage", currentPage)
                        else
                            print("Failed to advance to next page: ", result)
                            reject("Failed to advance to next page: " .. tostring(result))
                            return
                        end
                    else
                        reject("objectPage is not initialized or no more pages available")
                        return
                    end
                else
                    objectPage = AvatarEditorService:SearchCatalog(params)
                    currentPage = objectPage:GetCurrentPage()
                    print("Initialized objectPage and fetched currentPage")
                    print("Type of objectPage: ", typeof(objectPage))
                    print("Type of currentPage: ", typeof(currentPage))
                end

                if not currentPage then
                    warn("currentPage is nil")
                    reject("currentPage is nil")
                    return
                end

                if #currentPage == 0 then
                    warn("currentPage is empty")
                    reject("currentPage is empty")
                    return
                end

                print("Pages before send",objectPage.Name)
                store:dispatch(CatalogPage(objectPage))
                
                for _, value in ipairs(currentPage) do
                    table.insert(items, value)
                end

                print("Number of items in currentPage: ", #currentPage)
                print("Total items collected: ", #items)

                
                resolve(items)
            end)()
        end):andThen(function(assetTable)
            store:dispatch(FetchedCatalogIds(categoryIndex, subCategoryIndex, assetTable))
        end):catch(function(error)
            print("Something happened trying to get the current page: ", error)
        end)
    end
end

I’ve added some debug prints, and here are the logs:

Before calling AdvanceToNextPageAsync

Initialized objectPage and fetched currentPage - Client - FetchCatalogPage:61
21:09:54.143 Type of objectPage: Instance - Client - FetchCatalogPage:62
21:09:54.143 Type of currentPage: table - Client - FetchCatalogPage:63
21:09:54.143 Pages before send Instance - Client - FetchCatalogPage:78
21:09:54.143 Number of items in currentPage: 25 - Client - FetchCatalogPage:85
21:09:54.143 Total items collected: 25 - Client - FetchCatalogPage:86

After calling it:

21:10:40.353 Type of objectPage: Instance - Client - FetchCatalogPage:42
21:10:40.703 Initialized currentPage nil - Client - FetchCatalogPage:48
21:10:40.724 currentPage is nil - Client - FetchCatalogPage:67
21:10:40.725 Something happened trying to get the current page: currentPage is nil

AdvanceToNextPageAsync doesn’t return anything, to get the page that the Pages object is on, you would have to call pages:GetCurrentPage again

1 Like

I’m a beginner scripter, so I don’t know too much about pages yet. How do I know which page I’m on right now, and how do I get the next items?

Basically, replace this part:

                        local success, result = pcall(function()
                            return objectPage:AdvanceToNextPageAsync()
                        end)
                        if success then
                            currentPage = result

with

                        local success, result = pcall(function()
                            return objectPage:AdvanceToNextPageAsync()
                        end)
                        if success then
                            currentPage = objectPage:GetCurrentPage()

this should make currentPage a table with the items on the current page after advancing to that page.

2 Likes

Thank you so much for your help! now works perfectly. I really appreciate your assistance!

1 Like

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