Can someone tell me what im doing wrong?

Hello! I am using a script I found from another post, and currently I am stuck on a scripting error, so far I’ve tried to make it so that the script will choose a random asset ID from the data, however I cant seem to see what im doing wrong, if someone could tell me I would be grateful!

local HttpService = game:GetService("HttpService") -- enable httpservice by doing game.HttpService.HttpEnabled = true inside command prompt
local MPS = game:GetService("MarketplaceService") -- marketplaceservice

local function getLatestAsset()

	local success, assetId = pcall(function()
		local latestAssetId 

		local assetsResponse = HttpService:RequestAsync({
			Url = "https://catalog.roproxy.com/v1/search/items?category=Accessories&limit=120&sortType=3&subcategory=Accessories", -- the url
		})
		local assetsBody = HttpService:JSONDecode(assetsResponse.Body) -- get the body from our response(the body is the data that was returned)


		-- next we need to see if you request to roblox api failed, 
		-- if so, we throw an error which will be catched by our catch block

		if not assetsResponse.Success then
			error(string.format("%s %s", assetsResponse.StatusCode, assetsResponse.StatusMessage)) -- throw an error with the status code and status message
		end

	the stuff thats not working -->
	local item = math.random(1,#assetsBody.data)  
			local assetId = assetsBody[item]	
			local assetInfo = MPS:GetProductInfo(assetId, Enum.InfoType.Asset) <-- end of stuff thats not working
	
				latestAssetId = assetId
				script.Parent.SurfaceGui.Frame.Creator.Text = "By "..assetInfo.Creator.Name
				script.Parent.SurfaceGui.Frame.Title.Text = assetInfo.Name
				script.Parent.SurfaceGui.Frame.Description.Text = assetInfo.Description
				script.Parent.SurfaceGui.Frame.ImageIcon.Price.Text = assetInfo.PriceInRobux
				script.Parent.SurfaceGui.Frame.ImageIcon.Image = "https://assetgame.roblox.com/Game/Tools/ThumbnailAsset.ashx?aid="..assetId.."&fmt=png&wd=420&ht=420"
				
			
		
		return latestAssetId -- return latest assetId
	end)

	if not success then
		warn(string.format("Failed to get latest asset because: %s", assetId)) -- send a warning in the output to tell u request failed
	else 
		print(string.format("Got latest asset. Asset id: %d", assetId)) -- print latest asset id
	end		
end

while true do
	getLatestAsset()
	task.wait(10)
end

assetsBody[item] seems to only return a table with the id of the asset, and it’s type… (id, itemType)
image

To fix this, you can change the assetId variable to assetsBody[item].id, and of course, to get it’s type you can index the item with “itemType” (which would return Asset in this case?).

Precise Script
local HttpService = game:GetService("HttpService") -- enable httpservice by doing game.HttpService.HttpEnabled = true inside command prompt
local MPS = game:GetService("MarketplaceService") -- marketplaceservice

local function getLatestAsset()

	local success, assetId = pcall(function()
		local latestAssetId 

		local assetsResponse = HttpService:RequestAsync({
			Url = "https://catalog.roproxy.com/v1/search/items?category=Accessories&limit=120&sortType=3&subcategory=Accessories", -- the url
		})
		local assetsBody = HttpService:JSONDecode(assetsResponse.Body) -- get the body from our response(the body is the data that was returned)


		-- next we need to see if you request to roblox api failed, 
		-- if so, we throw an error which will be catched by our catch block

		if not assetsResponse.Success then
			error(string.format("%s %s", assetsResponse.StatusCode, assetsResponse.StatusMessage)) -- throw an error with the status code and status message
		end

		local item = math.random(1,#assetsBody.data)  
		local assetId = assetsBody[item].id	
		local assetInfo = MPS:GetProductInfo(assetId, Enum.InfoType.Asset) -- end of stuff thats not working

		latestAssetId = assetId
		script.Parent.SurfaceGui.Frame.Creator.Text = "By "..assetInfo.Creator.Name
		script.Parent.SurfaceGui.Frame.Title.Text = assetInfo.Name
		script.Parent.SurfaceGui.Frame.Description.Text = assetInfo.Description
		script.Parent.SurfaceGui.Frame.ImageIcon.Price.Text = assetInfo.PriceInRobux
		script.Parent.SurfaceGui.Frame.ImageIcon.Image = "https://assetgame.roblox.com/Game/Tools/ThumbnailAsset.ashx?aid="..assetId.."&fmt=png&wd=420&ht=420"



		return latestAssetId -- return latest assetId
	end)

	if not success then
		warn(string.format("Failed to get latest asset because: %s", assetId)) -- send a warning in the output to tell u request failed
	else 
		print(string.format("Got latest asset. Asset id: %d", assetId)) -- print latest asset id
	end		
end

while true do
	getLatestAsset()
	task.wait(10)
end

When running into problems like this, my suggestion is temporarily removing the pcall to check for errors in your scripts.

sorry for the late reply, but now it just says attempt to index nil with “id”

local item = math.random(1, #assetsBody.data)
local assetId = assetsBody.data[item].id
local assetInfo = MPS:GetProductInfo(assetId, Enum.InfoType.Asset)

You forgot to index the ‘data’ table, you also need to index the item’s ‘id’ member in order to retrieve its ID.

After implementating these fixes I received the following warning ‘attempt to index nil with ‘Parent’’ (because I’m lacking the necessary instances).

1 Like