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
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
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)
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
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
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;