Gamepass Retrieval of Player

  1. What do you want to achieve? I want to make a script that shows all the (public) gamepasses of a player.

  2. What is the issue? It doesn’t show all gamepasses. Also no errors.

  3. What solutions have you tried so far? I looked on the Forum and on YouTube to no avail.

Script:

local http = game:GetService("HttpService")
local DoubleId = false
local user

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

	local requestUrl = "https://www.roproxy.com/users/inventory/list-json?assetTypeId=34&cursor=&itemsPerPage=100&pageNumber="..pageNumber.."&userId="..user
	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
					local success3, result3 = pcall(function()
						for _, gamepass in ipairs(result2.Data.Items) do
							if gamepass.Creator.Id == user then
								for i, v in pairs(gamepasses) do
									if v == gamepass.Item.AssetId then
										DoubleId = true
									end
								end
								if not DoubleId then
									table.insert(gamepasses, gamepass.Item.AssetId)
								end
								DoubleId = false
							end
						end
						return gamepasses
					end)

					if success3 and result3 and result:len() ~= lastLength then
						lastLength = result:len()
						pageNumber += 1
						getGamepasses(plr, user, gamepasses, pageNumber, lastLength)
					end
				end
			else
				warn(result)
				getGamepasses(plr, user, gamepasses, pageNumber, lastLength)
			end
		end
	else
		warn(result)
		getGamepasses(plr, user, gamepasses, pageNumber, lastLength)
	end
	print(gamepasses)
	return gamepasses
end

game.ReplicatedStorage.GetGamepasses.OnServerInvoke = getGamepasses

Any help would be greatly appreciated!

1 Like

Use cursors instead(I also added type-checking for fun):

local HttpService = game:GetService("HttpService")

local function httpEnabled(): boolean
	local success, response = pcall(function()
		return HttpService:GetAsync("https://example.com/")
	end)
	if success then return true end
	if response == "Http requests are not enabled. Enable via game settings" then
		return false
	else
		return httpEnabled()
	end
end

if not httpEnabled() then
	warn("HttpService isn't enabled, enable from Game Settings")
	warn("If the game isn't published, run game.HttpService.HttpEnabled = true on command line")
	return 
end

local function constructUrl(url: string, params: {[string]: any}): string
	local paramStr = ""
	for key, val in pairs(params) do
		paramStr ..= key.."="..HttpService:UrlEncode(tostring(val)).."&"
	end
	return url.."?"..paramStr:sub(0, -2)
end

type creator = {CreatorProfileLink: string, HasVerifiedBadge: boolean, Id: number, Name: string, Type: number}
type item = {AbsoluteUrl: string, AssetCategory: number, AssetId: number, AssetType: number, 
	CurrentVersionId: number, IsApproved: boolean, LastUpdated: string, Name: string}
type product = {BcRequirement: number, Id: number, IsForSale: boolean, IsFree: boolean, IsLimited: boolean,
	IsLimitedUnique: boolean, IsPublicDomain: boolean, IsResellable: boolean, PriceInRobux: number, 
	SellerId: number, TotalPrivateSales: number}
type thumbnail = {Final: boolean, IsApproved: boolean, RetryUrl: string, Url: string}

type gamepass = {Creator: creator, Item: item, Product: product, Thumbnail: thumbnail}
type gamepassData = {Data: {End: number, Items: {gamepass}, ItemsPerPage: number, Page: number, 
PageType: string, Start: number, nextPageCursor: string, previousPageCursor: string}, IsValid: boolean}

local function getGamepasses(userId: number, gamepasses: {number}?, cursor: string?): {number}
	local gamepasses: {number} = gamepasses or {}
	local params = {assetTypeId = 34, cursor = cursor, itemsPerPage = 100, userId = userId}
	local url = constructUrl("https://www.roproxy.com/users/inventory/list-json", params)
	local success, result = pcall(function()
		return HttpService:GetAsync(url)
	end)
	if not success then
		warn(result)
		return getGamepasses(userId, gamepasses, cursor)
	end
	local data: gamepassData = HttpService:JSONDecode(result)
	if not data.IsValid then return gamepasses end
	for _, gamepass in pairs(data.Data.Items) do
		table.insert(gamepasses, gamepass.Item.AssetId)
	end
	local cursor = data.Data.nextPageCursor 
	if not cursor then return gamepasses end
	return getGamepasses(userId, gamepasses, cursor)
end

game.ReplicatedStorage.GetGamepasses.OnServerInvoke = function(plr: Player, userId: number)
	local gamepasses = getGamepasses(userId)
	--All gamepasses separated by comma
	print(table.concat(gamepasses, ", "))
end

The script only works for users with public inventories(unless you pass in an authentication cookie in the request headers that has access to the inventory you’re targeting).