How do you get a random asset from Roblox Catalog/Library?

I made a script for this a while ago and forgot to post it; this is basically the same thing instead I utilized a json table from a random word generating website (which had a lot more random words), randomized categories for broader results, and used AvatarEditorService, which allowed me to grab more items (120 max per request) using pagesObjects.

Do not use this script; it is just for example and has stuff I was using for my own purposes. It is poorly made and it could be made much better.

local httpService = game:GetService("HttpService")
local catalogSearchParams = CatalogSearchParams.new()
local avatarEditorService = game:GetService("AvatarEditorService")
local localValues = script:FindFirstChild("Local Values")
local dataStoreService = game:GetService("DataStoreService")
local itemStore = dataStoreService:GetDataStore("RItems")
local replicatedStorage = game:GetService("ReplicatedStorage")
local textService = game:GetService("TextService")
local players = game:GetService("Players")
local allitems = {}
--load data
game.Players.PlayerAdded:Connect(function(player)
	local owneditems = Instance.new("StringValue")
	owneditems.Parent = player
	owneditems.Name = "ownedItems"
	owneditems.Value = "{}"
	print("yippee")
end)
coroutine.wrap(function()
	local Success, Pages = pcall(function()
		return itemStore:ListKeysAsync()
	end)
	while true do
		local Items = Pages:GetCurrentPage()

		for _, Data in ipairs(Items) do
			local Key = Data.KeyName
			table.insert(allitems, Key)
		end
		task.wait(3)
		if Pages.IsFinished then break else
			local succ, err = pcall(function()
				Pages:AdvanceToNextPageAsync() 
			end) 
			if not succ then
				do
				task.wait(60)
				continue end
			end
		end
	end
end)()
wait(1)
local success, response = pcall(function()

	local data = httpService:JSONDecode(string.format(httpService:GetAsync("https://randomwordgenerator.com/json/words_ws.json"))) -- you can choose a different site, this one is a little limiting
	return data
end)

function getRandomKeyword()
	while response.data == nil do
		task.wait()
	end
	local rn = math.random(1, #response.data) 
	return response.data[rn].word.value 
end
function removeTableDupes(tab)
	local hash = {}
	local res = {}
	for _,v in ipairs(tab) do
		if (not hash[v]) then
			res[#res+1] = v
			hash[v] = true
		end
	end
	return res
end

function getItems()
	local assetTypes = { 
		Enum.AvatarAssetType.Hat,
		Enum.AvatarAssetType.BackAccessory,
		Enum.AvatarAssetType.FrontAccessory,
		Enum.AvatarAssetType.Gear,
		Enum.AvatarAssetType.HairAccessory,
		Enum.AvatarAssetType.WaistAccessory
	}

	catalogSearchParams.AssetTypes = {assetTypes[math.random(1, #assetTypes)]}
	catalogSearchParams.SearchKeyword = tostring(getRandomKeyword())
	catalogSearchParams.Limit = 120
	local originalword = catalogSearchParams.SearchKeyword
	local filteredword = textService:FilterStringAsync(catalogSearchParams.SearchKeyword, players:GetPlayers()[1].UserId)
	filteredword = filteredword:GetNonChatStringForUserAsync(players:GetPlayers()[1].UserId)
	if filteredword ~= originalword then
		getItems()
	else
		local success, pagesObject = pcall(function()
			local pagesObject = avatarEditorService:SearchCatalog(catalogSearchParams)
			return pagesObject
		end)
		if success then
			local currentPage = pagesObject:GetCurrentPage()
			
			if #currentPage == 0 then
				getItems() -- Reroll for new keyword
			else
				for i, v in pairs(currentPage) do
					currentPage[i]["Rarity"] = getRarity(currentPage[i])
					allitems = removeTableDupes(allitems)
					table.insert(allitems, currentPage[i].Id)
					local succ, err = pcall(function()
						itemStore:SetAsync(currentPage[i].Id, currentPage[i])
					end)
					task.wait()
				end
			end
		else
			warn(pagesObject)
			getItems()
		end
	end
end

function getRarity(item)
	local score = 0
	if item.CreatorName == "Roblox" then score += 1 end
	if item.CreatorHasVerifiedBadge then score += 1  end
	if item.FavoriteCount > 1000 then score += 1 end
	if item.FavoriteCount > 10000 then score += 1 end
	if item.ItemRestrictions.Collectible then score += 1 end
	if item.ItemRestrictions.LimitedUnique then score += 1 end
	if score == 0 then return "Common" end
	if score == 1 then return "Uncommon" end
	if score == 2 then return "Rare" end
	if score == 3 then return "Epic" end
	if score == 4 then return "Legendary" end
	if score == 5 then return "Mythical" end
	if score == 6 then return "Godly" end
end
--get data
replicatedStorage.Data.RetrieveData.OnServerInvoke = function(player)
	return allitems
end
replicatedStorage.Data.GetItemData.OnServerInvoke = function(player, itemId)
	local item = itemStore:GetAsync(itemId)
	local data = httpService:JSONDecode(player.ownedItems.Value)
	print(data)
	table.insert(data, item)
	player.ownedItems.Value = httpService:JSONEncode(data)
	return item
end
replicatedStorage.Data.SaveData.OnServerInvoke = function(player, ownedItems)
	itemStore:SetAsync(player.UserId, ownedItems)
end
getItems()
coroutine.wrap(function()
	while wait(10) do
		getItems()
	end
end)()

game:BindToClose(function() -- get rid of annoying lag and crashes for some reason (band aid fix i know)
	script.Disabled = true
end)
1 Like