local httpService = game:GetService("HttpService")
local function GetTotalRAP(userId)
local success, msg = pcall(function()
local total = 0
local function CollectRAP(cursor)
local url = "https://inventory.rprxy.xyz/v1/users/" .. userId .. "/assets/collectibles?sortOrder=Asc&limit=100"
if cursor then
url = url .. "&cursor=" .. cursor
end
local data = httpService:GetAsync(url)
data = httpService:JSONDecode(data)
for i = 1, #data["data"] do
pcall(function()
total = total + data["data"][i]["recentAveragePrice"]
end)
end
if data["nextPageCursor"] then
CollectRAP(data["nextPageCursor"])
end
end
CollectRAP()
return total
end)
if not success then
warn(msg)
else
return msg
end
end
print(GetTotalRAP(game.Players:GetUserIdFromNameAsync("ASTROCPF")))
game.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 = 0
RAP.Parent = Leaderstats
end)
When doing web requests always have in mind that they can fail at any given time. To combat this you can retry a request if it fails.
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)