How do I check if an asset ID is valid?

Hey guys - quick question. I’m trying to validate whether a given asset ID is valid or not, but I’m not sure which Service or function is the best for doing so. I could try preloading it with ContentProvider, but that seems weird to me. I could also try inserting it with InsertService, but I’m not sure it’s worth all the hassle of making a Model with it inside. I also want to validate that the asset ID refers to an image, and not a model or any other kind of asset.

Any ideas?

Don’t get me wrong either - if ContentProvider or InsertService are really the best options, I’m open to suggestions on using them too.

6 Likes

I believe you could use GetProductInfo with the Asset InfoType. As the api-reference specifies it’ll return this error if it doesn’t exist:

MarketplaceService:getProductInfo() failed because HTTP 0 (HTTP 400 (HTTP/1.1 400 BadRequest))

3 Likes

I mean you can try this but I’m sure there’s a better way possibly:

local Id = 123456
local Success = pcall(function()
     game:GetService("MarketplaceService"):GetProductInfo(Id)
end)

if (Success) then
    print("Valid AssetId")
else
    print("Invalid AssetId")
end
5 Likes
local Marketplace = game:GetService("MarketplaceService")
--later
--method call replacement: a:b(...) == a.b(a, ...)
local valid = pcall(Marketplace.GetProductInfo, Marketplace, assetId)
--if valid then or if pcall(Marketplace.GetProductInfo, Marketplace, assetId) then
3 Likes

Thanks but he seems like a starter so that might confuse him. :expressionless:

1 Like

It’s not confusing. Functions are first-class. Also you can get the results of the function passed if there was no exception.

Something like:

local ok, info = pcall(MarketplaceService.GetProductInfo, MarketplaceService, asset)

-- info is the table or the string message if something went wrong
8 Likes

Thanks for your input, but please don’t either assume nor insult my intelligence and/or experience. I’ve been scripting for many years, I’ve just never come across this particular problem and was looking for a pointer in the correct direction. I know exactly what a pcall and functions do. These are great suggestions - I will try them out soon and come back with results.

12 Likes

RESOLVED

Colon was left out ‘:GetProductInfo’

userInput.TextBoxFocusReleased:Connect(function(EnterPressed)
	if EnterPressed then
		local newCursor = tonumber(searchBar.Text)
		local succ = pcall(function()
			game:GetService("MarketplaceService"):GetProductInfo(newCursor)
		end)
		if succ then
			print('Success')
		end
	end
end)