Iterating through all pages with GetDevelperProductsAsync

Hello! I am trying to use GetDeveloperProductsAsync to write out each and every developer product I own, but currently I only get one page’s worth of information. How would I iterate through each page to get all information for every developer product?

Code from Devforum:

local developerProducts = game:GetService("MarketplaceService"):GetDeveloperProductsAsync():GetCurrentPage()
 
for _, developerProduct in pairs(developerProducts) do
    for field, value in pairs(developerProduct) do
        print(field .. ": " .. value)
    end
    print(" ")
end
1 Like

You need to use AdvanceToNextPageAsync since GetDeveloperProductsAsync returns a Pages object

The Pages article provides some code that could be used for you to iterate through all the pages

function iterPageItems(pages)
	return coroutine.wrap(function()
		local pagenum = 1
		while true do
			for _, item in ipairs(pages:GetCurrentPage()) do
				coroutine.yield(item, pagenum)
			end
			if pages.IsFinished then
				break
			end
			pages:AdvanceToNextPageAsync()
			pagenum = pagenum + 1
		end
	end)
end

And to use it, you’d just do

for item, pageNo in iterPageItems(myPageObject) do
	-- look at item. Pages will advance automatically
end

Where myPageObject is a Pages object

Hi! I forgot to mention I tried this with the code below and I had an error.

Code:

local developerProducts = game:GetService("MarketplaceService"):GetDeveloperProductsAsync()

function iterPageItems(pages)
	return coroutine.wrap(function()
		local pagenum = 1
		while true do
			for _, item in ipairs(pages:GetCurrentPage()) do
				coroutine.yield(item, pagenum)
			end
			if pages.IsFinished then
				break
			end
			pages:AdvanceToNextPageAsync()
			pagenum = pagenum + 1
		end
	end)
end


for _, developerProduct in iterPageItems(developerProducts) do
	for field, value in pairs(developerProduct) do
		print(field .. ": " .. value)
	end
	print(" ")
end

which returns

invalid argument #1 to 'pairs' (table expected, got number)

Do you know how I would fix that?

It’s because the first thing given using that method is the item, the 2nd is the page number, try flipping your developerProduct and _ around

1 Like

Hi! Thanks for your response.

I tried this but got an error explaining that I attempted to index nil with GetCurrentPage().

Do you think you could provide an example code? I am not sure if we are on the same page.

I’m not sure if something is going on, what happens if you print the 2 things that you get from

for _, developerProduct in iterPageItems(developerProducts) do
	for field, value in pairs(developerProduct) do
		print(field .. ": " .. value)
	end
	print(" ")
end

For now comment out that other in pairs loop and see what happens when you do this

for devProduct, pageNo in iterPageItems(developerProducts) do
	print(devProduct,pageNo)
	print(" ")
end
1 Like

Hello! I tried this but it didn’t print out all the developer products, just about 10-15 of the first ones.

I do think it would be nice to mention I am working with 10k developer products (i know, stupidly high amount) at the moment.
image

Could that be contributing to it not working?

Or does the console have a limit on the amount it can print?

I found something from a post relating to what you’d need, maybe try this out?

local developerProducts = game:GetService("MarketplaceService"):GetDeveloperProductsAsync()

while not developerProducts.IsFinished do
	wait()
	local data = developerProducts:GetCurrentPage()
	
	for field, val in pairs(data) do
		print(field .. ": " .. val)
	end
	
	developerProducts:AdvanceToNextPageAsync()
end

I think it should work for what you need hopefully

3 Likes

For anyone in the future who may find this post, there’s a bug in the code above. It doesn’t ever actually iterate the final page. I’ve created this (messy) module that will accomplish the task and has a retry functionality built-in in case it errors.

function iteratePages()
		
	local t = {};
	local developerProducts = game:GetService("MarketplaceService"):GetDeveloperProductsAsync()

	while true do
		
		local data = developerProducts:GetCurrentPage()

		for field, val in (data or {}) do
			t[#t+1] = val;
		end
		
		if developerProducts.IsFinished then
			break;
		else
			developerProducts:AdvanceToNextPageAsync()	
		end
		
	end
	
	return t;
	
end
	
local succ,list = nil;
while not succ or not list do
	succ,list = pcall(iteratePages);
	if not succ or not list then
		task.wait(1)
	end
end

return list;
5 Likes