How to decode this data right?

Hi, i’ve read the documentation on the External Catalog API Documentation, and in this code, i tried to get a random decal from the catalog and get information from it, and i can’t seem to get it to work? (The data extraction doesn’t work)
here’s the function:

function module.GetRandomDecal()
	local decalData = {}
	local success = false

	while success == false do
		pcall(function() 
			local url = "https://search.roblox.com/catalog/json?Category=8"
			local response = HttpService:GetAsync(url)
			local data = HttpService:JSONDecode(response)

			if data and data.data then
				local decalInfo = data.data[math.random(1, #data.data)]
				if decalInfo and decalInfo.assetId then
					decalData.id = decalInfo.assetId
					decalData.creatorID = decalInfo.creatorTargetId
					decalData.favoriteCount = decalInfo.favoriteCount
					decalData.creationDate = decalInfo.createdDate
					success = true
				end
			end
		end)

		task.wait()
	end

	return decalData.id, decalData
end

The issue isn’t the decoding but the fetching of information. Your query returns 0 results(an empty array). This is because you should pass the Keyword parameter in the url with a query of at least 2 characters long. Since this is supposed to be a random decal function, you can make another function for generating a random query and passing that:

local function pickRandomLetter(alphabet: string): string
	local x = math.random(1, alphabet:len())
	return alphabet:sub(x, x)
end

--modify this as you wish
local function generateRandomQuery(queryLen: number): string
	--characters to use in random query
	local alphabet = "abcdefghijklmnopqrstuvwxyz0123456789"
	local result = ""
	for i = 1, queryLen do result ..= pickRandomLetter(alphabet) end
	return result
end

local keyword = generateRandomQuery(2)
local url = "https://search.roblox.com/catalog/json?Category=8&Keyword="..keyword
--fetch the url with the randomized query here
1 Like

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