How to optimize my api usage

mine ran

20:37:36.395  Failed to retrieve item details for item ID: 1532396  -  Server - RollServer:51
  20:37:36.395  Error message: HTTP 429 (Too Many Requests)  -  Server - RollServer:52
  20:37:36.484  Failed to retrieve item details for item ID: 1533893  -  Server - RollServer:51
  20:37:36.484  Error message: HTTP 429 (Too Many Requests)  -  Server - RollServer:52
  20:37:36.571  Failed to retrieve item details for item ID: 1563352  -  Server - RollServer:51
  20:37:36.571  Error message: HTTP 429 (Too Many Requests)  -  Server - RollServer:52
  20:37:36.660  Failed to retrieve item details for item ID: 1594180  -  Server - RollServer:51
  20:37:36.660  Error message: HTTP 429 (Too Many Requests)  -  Server - RollServer:52
  20:37:36.748  Failed to retrieve item details for item ID: 1861997  -  Server - RollServer:51
  20:37:36.748  Error message: HTTP 429 (Too Many Requests)  -  Server - RollServer:52
  20:37:36.835  Failed to retrieve item details for item ID: 2264398  -  Server - RollServer:51
  20:37:36.835  Error message: HTTP 429 (Too Many Requests)  -  Server - RollServer:52
  20:37:36.982  Failed to retrieve item details for item ID: 2799053  -  Server - RollServer:51
  20:37:36.982  Error message: HTTP 429 (Too Many Requests)  -  Server - RollServer:52
  20:37:37.072  Failed to retrieve item details for item ID: 3271938  -  Server - RollServer:51
  20:37:37.072  Error message: HTTP 429 (Too Many Requests)  -  Server - RollServer:52
  20:37:37.162  Failed to retrieve item details for item ID: 4765323  -  Server - RollServer:51
  20:37:37.162  Error message: HTTP 429 (Too Many Requests)  -  Server - RollServer:52
  20:37:37.255  Failed to retrieve item details for item ID: 4765718  -  Server - RollServer:51
  20:37:37.255  Error message: HTTP 429 (Too Many Requests)  -  Server - RollServer:52
  20:37:37.345  Failed to retrieve item details for item ID: 5013615  -  Server - RollServer:51
  20:37:37.345  Error message: HTTP 429 (Too Many Requests)  -  Server - RollServer:52

etc

I have the same output
(character limit too)

but it seems to be requestion the api evertime for each id

I remade a script and i have no errors now:

local HttpService = game:GetService("HttpService")
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local rollFunc = ReplicatedStorage.RollFunc
local rollR = ReplicatedStorage.Roll
local Chances = require(script.Chances)
local itemRarities = require(script.ModuleScript)

local function performMultipleRandomRolls(assets, numRolls)
		local results = {}
		-- Clear selectedCategorys table before performing new rolls
		selectedCategorys = {}

		for _ = 1, numRolls do
			local totalWeight = 0
			local randomValue = math.random() * 161.1601  -- Assuming your probabilities are in percentages

			-- Determine the selected category based on the random value
			for _, category in ipairs(Chances) do
				totalWeight = totalWeight + category[2]
				if randomValue <= totalWeight then
					table.insert(selectedCategorys, category)  -- Insert entire category into selectedCategorys
					if #selectedCategorys > 5 then
						table.remove(selectedCategorys, 1)  -- Remove the oldest entry if more than 5 categories are stored
					end
					break
				end
			end

			if selectedCategorys[#selectedCategorys][1] then
				-- Retrieve the appropriate list of item IDs based on the selected category
				local categoryItems = itemRarities[selectedCategorys[#selectedCategorys][1]]
				if categoryItems and #categoryItems > 0 then
					-- Select a random item ID from the category
					local randomIndex = math.random(1, #categoryItems)
					local selectedItemId = categoryItems[randomIndex]
					table.insert(results, selectedItemId)
				else
					table.insert(results, nil)  -- No items in the selected category
				end
			else
				table.insert(results, nil)  -- Invalid category selected
			end
		end

		return results
	end

local function run()
	local success, response = pcall(HttpService.GetAsync, HttpService, "https://api.rolimons.com/players/v1/playerassets/1")
	if not success then
		warn("Failed to retrieve player assets:", response)
		return
	end

	local data = HttpService:JSONDecode(response)
	if not data or not data.playerAssets then
		warn("Invalid player assets data:", response)
		return
	end

	local newAssets = {}
	for id, _ in pairs(data.playerAssets) do
		table.insert(newAssets, tonumber(id))
	end
	table.sort(newAssets)

	-- Fetch item details
	local success1, response1 = pcall(HttpService.GetAsync, HttpService, "https://www.rolimons.com/itemapi/itemdetails")
	if not success1 then
		warn("Failed to retrieve item details:", response1)
		return
	end

	local decodedResponse = HttpService:JSONDecode(response1)
	if not decodedResponse or not decodedResponse.items then
		warn("Invalid item details response:", response1)
		return
	end

	-- Process item details
	local cachedItemDetails = {}
	for itemId, _ in pairs(data.playerAssets) do
		if decodedResponse.items[tostring(itemId)] then
			cachedItemDetails[itemId] = decodedResponse.items[tostring(itemId)]
		else
			warn("Item details not found for ID:", itemId)
		end
	end

	-- Process limited items and categorize them
	local rapValues = {}
	for _, itemId in ipairs(newAssets) do
		local itemData = cachedItemDetails[itemId]
		if itemData then
			local rap = itemData[3] or 0  -- Default to 0 if RAP is missing
			table.insert(rapValues, rap)
		else
			table.insert(rapValues, 0)  -- Placeholder for missing item data
		end
	end

	-- Categorize items into rarity tiers based on RAP values
	for rarity, _ in pairs(itemRarities) do
		itemRarities[rarity] = {}
	end

	local numItems = #newAssets
	for i = 1, numItems do
		local itemId = newAssets[i]
		local rap = rapValues[i]

		if rap > 0 then
			if rap <= 5000 then
				table.insert(itemRarities.Common, itemId)
			elseif rap <= 10000 then
				table.insert(itemRarities.Uncommon, itemId)
			end
		end
	end

	-- Perform delayed roll function
	task.wait(3) -- Wait for 3 seconds
	rollFunc.OnServerInvoke = function(player)
		local rolls = 5
		local selectedAssetIds = performMultipleRandomRolls(newAssets, rolls)

		-- Prepare results to send to the client
		local rapValues = {}
		local defValues = {}
		for i = 1, 5 do
			local itemId = selectedAssetIds[i]
			if itemId then
				local itemData = cachedItemDetails[itemId]
				if itemData then
					local rap = itemData[3] or 0
					local defValue = itemData[5] or 0
					table.insert(rapValues, rap)
					table.insert(defValues, defValue)
				else
					table.insert(rapValues, 0) -- Placeholder if data is missing
					table.insert(defValues, 0)
				end
			else
				table.insert(rapValues, 0) -- Placeholder for nil asset ID
				table.insert(defValues, 0)
			end
		end

		-- Fire client event with results
		rollR:FireClient(player, selectedAssetIds, rapValues, selectedCategorys, defValues)
		print(player, selectedAssetIds, rapValues, selectedCategorys, defValues)
	end
end

run()

It worked for me but i dont know will it work for you

no errors but all the ui change is on the server and now none of the ui is changing
robloxapp-20240421-2054356.wmv (130.0 KB)

I think thats because the func runs only one time

When you press roll button, what are you firing (event or function?)

both
(thank you so much for bearing with me for last 2 hours)

Its ok :slight_smile:

(character limit again)

I maded a little gui like just one button and i clicked two times, here is the output:

Thanks, can you also send a screenshot of how your gui works because ill recreate it so your script will work

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