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

  1. What do you want to achieve? Keep it simple and clear!
    I want to be able to access a random asset from Roblox’s Catalog/Library(assets such as videos and sounds)

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried using Proxy Service from these topics (Inserting EVERY hat in existence, How would I get a table of random hats? - #2 by Doqee), but I find no luck understanding how to do this. I also tried using InsertService:GetCollection() but the documentation isn’t really helpful. I am also aware of GetFreeDecals and GetFreeModels functions but these are just for some of the asset categories.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- This is what I have so far using ProxServ, video library was able to be printed
local ProxServ = require(game.ServerScriptService.ProxyService)
local InsertServ = game:GetService("InsertService")

local rand = Random.new()
local Proxy = ProxServ:New(
	'secret', 
	'secret'
)

local url = "https://search.roblox.com/catalog/json?CatalogContext=2&"
local params = "Category=14"
url = url..params


local VideoLibrary = ProxServ:Get(url)
local randomVid = VideoLibrary[rand:NextInteger(1,#VideoLibrary)]
print(url)
print(VideoLibrary)
print(randomVid )

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

2 Likes

You can load assets using the insert service by their ID with:

model = game:GetService('InsertService'):LoadAsset(ASSET_ID)

The above works for models, accessories, and anything else that might be stored as rbxm style data. You still need to have permission to insert the asset. There might also be permission issues with trying to insert in a live server to prevent exploits, but I’m not sure.

You can get information about assets using the marketplace service. I’d look through the API’s on that and see if you find what you need.

2 Likes

thanks for the reply, but the problem is getting an asset id randomly.

You’ll want to make a list of assets that you can pull from randomly, since most assets won’t have permission to be inserted. That is a bit of a custom job. You’ll need someone to help you scrape assets dynamically, or populate a list by hand.

1 Like

This topic is still not yet answered, if there are experts with Roblox’s API then help would be great.

1 Like

This is a very late reply but i managed to get random items by putting a random keyword with a random number.

local httpService = game:GetService("HttpService")
local insertService = game:GetService("InsertService")
local Marketplace = game:GetService("MarketplaceService")

local Category = "Clothing"
local Subcategory = "ClassicShirts"
local Limit = "10"

local randomwords = {"pink","yellow","blue","purple","green","beige","black","white","emerald","saphir",
	"rubin","casual","boring","pretty","nice","almost","funny","scary","emo","girly","aesthetic","red",
	"orchid","tulip","flower","rose","amazing","water","flow","sea","river","boys","girls","women","men",
	"all","small","big","hero","french","italian","australian","female","male","king","queen","oof",
	"real","godly","goofy","animal","sad","happy","emotion","selfmade","roblox","classic","brick",
	"dead","groovy","heart","loving","armor","night","day","afternoon","noon","time","weekly",
	"background","back","lining","canvas","title","paper","girly","neat","best","depressed","align",
	"note","binder","edge","blanket","blanks","bleeding","blind","stamp","stamping","paper","body",
	"kern","knock","craft","art","useless","colorful","star","furry","copy","copied","daily",
	"mexican","american","ethical","karma","criminal","abnormal","caring"
}

local Keyword = randomwords[math.random(1,#randomwords)]..tostring(math.random(1,9999))

local RequestUrl = "https://catalog.roproxy.com/v1/search/items/details?category=Clothing&subcategory="..Subcategory.."&Limit="..Limit.."&keyword="..Keyword

local Success, Result = pcall(httpService.GetAsync, httpService, RequestUrl)
if Success then
	local success2, result2 = pcall(httpService.JSONDecode, httpService, Result) 
	if success2 then
		for _, v in pairs(result2.data) do
			local info = Marketplace:GetProductInfo(v.id)
			local asset = insertService:LoadAsset(info.AssetId)
			print(info.AssetId)
			for _, v in pairs(asset:GetChildren()) do
				print(v.Name)
			end
			break
		end
	end
end

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