Hello again! I am trying to figure out a way to check to see if a audio ID entered by a user is actual audio ID or just random numbers. I thing preload asset might be in the right direction but I have no idea from the point. Any help would be great!
Run the passed ID through MarketplaceService.GetProductInfo. Make sure to specify the second argument as Enum.InfoType.Asset. From there, check if AssetTypeId is 3. If it is, it’s an audio asset.
local assetInfo = MarketplaceService:GetProductInfo(1337, Enum.InfoType.Asset)
if assetInfo.AssetTypeId == 3 then
print("Audio")
end
The issue im having with this is if the value that is entered, isn’t a Roblox Asset then it just sends a error. I want it to check to see if it’s a audio with any string or number and send back yes or no
Try putting that code in a pcall() to catch any errors that are produced. Here’s what it’d look like with colbert’s code from above.
local success, result, assetInfo = pcall(function() -- This prevents any errors from stopping the code
assetInfo = MarketplaceService:GetProductInfo(your_Id, Enum.InfoType.Asset)
end)
if success and assetInfo and assetInfo.AssetType == 3 then -- If there was no errors and the asset type is an audio
print("It's an audio!")
elseif not success then
warn(result) -- Warn the error message to the output
end
Just a note on the way you used pcall there; you can actually just return out what GetProductInfo returns or directly wrap the function instead of declaring another local variable.
local success, result = pcall(function ()
return MarketplaceService:GetProductInfo(1337, Enum.InfoType.Asset)
end)
local success, result = pcall(MarketplaceService.GetProductInfo, MarketplaceService, 1337, Enum.InfoType.Asset)
Oh ya, I forgot about that. Thanks
The only problem I have now is it stops at the Pcall and doesnt continue.
local function IsValidAudio(AudioId)
if typeof(AudioId) == "number" then
local Success, AssetInfo = pcall(game.MarketplaceService.GetProductInfo, game.MarketplaceService, AudioId)
return Success and AssetInfo.AssetTypeId == 3
end
end