Help and Feedback, Scripting Support | Finished

Hello, I am currently trying to make a radio ui that doesn’t give an error when the player inserts a wrong sound id.
The problem is that I can’t find a way to prevent the error from showing in the output.
I thought that putting the code in a pcall() would stop the error, but it didn’t. After that I thought that I might read some free models for advice but those didn’t help also.

Here is the code I used:

Play.MouseButton1Click:Connect(function()
	Sound.SoundId = "rbxassetid://"..MusicId.Text
	Sound:Play()
end)

When I test my game in the output it says: Failed to load sound rbxassetid://WrongSoundId: Unable to download sound data (“WrongSoundId” was what I put in the Textbox)

Is there any way to prevent this error?

I think that this error occurs because you either don’t have permission to use the audio, or the audio was denied in moderation.

1 Like

To prevent players entering invalid audio, you have to check that it is audio with MarketplaceService:GetProductInfo(). Here’s the documentation for it.

local market = game:GetService("MarketplaceService")

Play.MouseButton1Click:Connect(function()
    if tonumber(MusicId.Text) then
        local productInfo = market:GetProductInfo(tonumber(MusicId.Text))
        if productInfo.AssetTypeId == 3 then --i think 3 is audio, im not sure though
        	Sound.SoundId = "rbxassetid://"..MusicId.Text
	        Sound:Play()
        else
        --the text was a number, but isnt an id of audio
        end
    else
        --the text isnt a number
    end
end)
3 Likes

This is the most interesting chat I’ve seen, seeming as its two people with the relatively same avatar talking.

A solution you might want to do instead is detecting if the sound is loaded, if it hasn’t loaded then you can assume its an InvalidID, you can then use properties to determine if it is a sound or not (as it wont load its properties):
https://developer.roblox.com/en-us/api-reference/event/Sound/Loaded
https://developer.roblox.com/en-us/api-reference/property/Sound/IsLoaded

3 Likes