My script works outside the While loop but not within?

So, I got this code that only works outside the “While XXX do” loop and returns nil within it. Any idea what’s going on or just ROBLOX wonkiness?

local Max_Catalog_Items = 125
local Max_Page_Tries = 200
	
local catalogSearchParams = CatalogSearchParams.new()
catalogSearchParams.MinPrice = 5
catalogSearchParams.CreatorName = self.Player.Name

local currentPageObject = AvatarEditorService:SearchCatalog(catalogSearchParams)
local foundItems = 0
local pagesTried = 1
local currentPage

while not currentPageObject.IsFinished do
	currentPage = currentPageObject:GetCurrentPage()

	for _, data in ipairs(currentPage) do
		foundItems += 1
		local Price = data["Price"]
		local ItemId = data["Id"]
		local ItemType = data["ItemType"]
		local ItemName = data["Name"]

		local Description = data["Description"]
		local FavoriteCount = data["FavoriteCount"]

		local NewData = Instance.new("NumberValue", AssetFolder)
		NewData.Name = ItemType..":"..Description..":"..FavoriteCount..":"..ItemId.. ":".. ItemName
		NewData.Value = Price

		if foundItems >= Max_Catalog_Items then
			break
		end
	end

	pagesTried = pagesTried + 1
	currentPageObject:AdvanceToNextPageAsync()
end
warn("FINISHED")
warn(currentPage)

“nil” as said, is returned inside “while” and actual :GetCurrentPage() when outside it.

Maybe you are overstressing it and reaching limit of requests?
You mean that currentPageObject:GetCurrentPage() method returns nil?

Nothing in the docs mentions rate limits, if I’m reading it correctly the methods just yield until the data is ready.

@Leppux does the inner loop run at all, or is it erroring out immediately? If so, how many times does it run, and how many times do you expect it to?

My hunch is that currentPageObject:AdvanceToNextPageAsync() is returning immediately, and on the final page there is a race condition on the currentPageObject.IsFinished property. That is, your loop “successfully” checks that its not Finished before AdvanceToNextPageAsync() sets the property to true.

The API documentation implies that this can’t be true, as it says that AdvanceToNextPageAsync() yields, but the ‘Async’ in the name implies otherwise. Additionally, IsFinished should be set to true on the last page, so your loop body actually needs to execute one more time when the condition becomes false – but, again, that doesn’t make sense here that would mean GetCurrentPage() is returning nil on the second-to-last page.

The loop doesn’t run at all, since :GetCurrentPage() is nil inside the while loop. If I take the exact same code, but without the While Loop it does return a table.

Also, I’ve made it after ROBLOXs own example of the usage of this. Exact same setup, just different search criteria.

If IsFinished is removed, it errors due to :AdvanceToNextPageAsync()

After more documentation reading, I found out that .IsFinished means, that it is on the last page available. So, I change the bottom code to this:

pagesTried = pagesTried + 1
if currentPageObject.IsFinished then
	IsDone = true
	break
end
currentPageObject:AdvanceToNextPageAsync()

This has made it, so it works and keeps the same functionality of the previous thing.

1 Like

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