Invalid argument #1 to 'ipairs' Table expected, got nil

I took some example code from another topic on how you can get game passes, etc from a player using RoProxy however when I change the AssetTypeId to 5 this error happens.

(5 means a Lua Asset)

Credits to @Forummer for the code.


[Redacted site since it is a phishing URL for some reason]

Error that happens:

Line that errored:
Capture

local http = game:GetService("HttpService")

local baseUrl = "https://www.roproxy.com/users/inventory/list-json?assetTypeId=5&cursor=&itemsPerPage=100&pageNumber=%s&userId=%s"

local function getUserCreatedGamepassesRecursive(userId, gamepasses, pageNumber, lastLength)
	gamepasses = gamepasses or {}
	pageNumber = pageNumber or 1
	lastLength = lastLength or math.huge

	local requestUrl = baseUrl:format(pageNumber, userId)
	local success, result = pcall(function()
		return http:GetAsync(requestUrl)
	end)

	if success then
		if result then
			local success2, result2 = pcall(function()
				return http:JSONDecode(result)
			end)

			if success2 then
				if result2 then
					for _, gamepass in ipairs(result2.Data.Items) do
						if gamepass.Creator.Id == userId then
							table.insert(gamepasses, gamepass.Item.AssetId)
						end
					end

					if result:len() ~= lastLength then
						lastLength = result:len()
						pageNumber += 1
						getUserCreatedGamepassesRecursive(userId, gamepasses, pageNumber, lastLength)
					end
				end
			else
				warn(result)
				getUserCreatedGamepassesRecursive(userId, gamepasses, pageNumber, lastLength)
			end
		end
	else
		warn(result)
		getUserCreatedGamepassesRecursive(userId, gamepasses, pageNumber, lastLength)
	end
	return gamepasses
end

local userGamepasses = getUserCreatedGamepassesRecursive(2032622)
print(#userGamepasses) --6 gamepasses.
for _, gamepassId in ipairs(userGamepasses) do
	print(gamepassId) --6 gamepass IDs.
end

How can I fix the line, so it doesn’t error?

As stated in the error message, “(table expected, got nil),” implying that index “Items” has no value.

How would I fix the line though?

It’s quite simple. Just use an if statement:

if result2.Data.Items then
	...
end

Surely fixed the error but another problem is Lua Assets return ‘0’ even though I have Lua Assets in my inventory. (I’m testing it with CreatorId)

Did some further debugging the for loop doesn’t even run??

Did a print below the if statement and it didn’t run the print.

It will only execute the process if the condition is true.

The condition probably isn’t true causing it to not execute.

1 Like

If the condition is not true, then the issue is coming from your URL.

I tried using your URL with page number 1 and my user ID. The result was asset type does not exist.

{"IsValid":false,"Data":"AssetType does not exist."}

How would I fix that issue coming from the URL?

Anything below 8 will cause the error I found out; how can I prevent this?

1 Like