How to get RAP of a player (total avatar price)

My code: local httpService = game:GetService(“HttpService”)
local players = game:GetService(“Players”)
local maxRetries = 50

local function CollectRAP(userId)
local total = 0
local cursor = nil

while true do
	local url = "https://inventory.roproxy.com/v1/users/" .. userId .. "/assets/collectibles?sortOrder=Asc&limit=100"
	if cursor then
		url = url .. "&cursor=" .. cursor
	end

	local data, success = nil, false
	for i = 1, maxRetries do -- retry
		data = httpService:GetAsync(url)
		success = pcall(function() data = httpService:JSONDecode(data) end)
		if success then break end
		wait(1) -- wait 1 second before retrying
	end

	if not success then
		error("Failed to retrieve data after" .. tostring(maxRetries) .. "attempts for player " .. players:GetNameFromUserIdAsync(userId))
	end

	for i, assetData in ipairs(data.data) do
		total = total + (assetData.recentAveragePrice or 0)
	end

	if not data.nextPageCursor then
		break
	end

	cursor = data.nextPageCursor
end

return total

end

local function GetTotalRAP(userId)
if type(userId) ~= “number” then
error(“Invalid user ID for GetTotalRAP”)
end

local success, total = pcall(CollectRAP, userId)
if not success then
	warn("Failed to retrieve RAP for player " .. players:GetNameFromUserIdAsync(userId) .. ": " .. total)
	return nil
end

return total

end

players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new(“Folder”)
Leaderstats.Name = “leaderstats”
Leaderstats.Parent = Player
local RAP = Instance.new(“NumberValue”)
RAP.Name = “RAP”
RAP.Value = GetTotalRAP(Player.UserId)
RAP.Parent = Leaderstats
end)

My error: HTTP 401

How to fix?

1 Like

HTTP 401 is unauthorized access to the url.

Users who have their inventory not publicly disabled will always return this error.
Maybe this is why you’re getting that error? or it could also be because of the proxy you’re using.