How to check if a song ID is invalid

I’m not really sure if Roblox provides a service for this. But I wanted to open a discussion about how to detect if a SoundID is invalid.

3 Likes

not for sure but maybe if you could do some sort of if statement to see if an audio loaded

this might help

You can tell if a “Song ID” is invalid by seeing if it says [Content Deleted] or Remove for copyright, anything else that is not content deleted or copyrighted should be valid.

For some reason, when I type random song values into my queueing system, it brings up random song names but it doesn’t actually play the songs. I know I can’t remove it from my system because it will display the name of the song when it plays and I got a song called “SolidModel” with no music playing at all. I just want to be able to detect when no music is playing even though technically, it is playing.

By “invalid”, do you mean determining if the ID isn’t an Audio asset (for example, if the ID leads to a Shirt or something), or determining if it’s a copyrighted/deleted Audio?

If it leads to something other than a SoundID yes.

You could definitely use InsertService for this

local assetId = 257489726
local InsertService = game:GetService("InsertService")
local model = InsertService:LoadAsset(assetId)
local sound = model:GetChildren()[1]

if sound:IsA("Sound") then
-- we've got it
end
2 Likes

I feel like that’s searching for if something is a Sound. We’re focusing on if the SoundID is valid

Sorry I thought you may have been inserting the wrong type of asset. Make sure your system does a check either way.

You can tell if a sound is “valid” by checking if its TimeLength is greater than zero. Just run a comparison like this:

if sound and (sound.TimeLength > 0)
-- valid enough for me!
end
1 Like

Would just test the soundID work? Just putting it into a Sound part and seeing if it plays the song, that might work. I mean if it doesn’t play then it can’t be valid right?

Well I’m making a MusicSystem where players can add songs to queue, I just don’t want a song to be playing even though no sound can be heard. I want to make the system convenient

Even still, if the player puts the ID into the music player and it doesn’t play, that would mean it’s invalid right?

Yes it would be invalid, however there is no way to skip the song. It will just play through as if it was a regular audio, but no one would be able to hear anything. That’s what I’m trying to stop. I want the server to automatically skip it when it happens. I wonder if there’s a certain ID that if the SoundID is below it an ID, it will be invalid automatically

You can use this method of marketplaceservice

local MPS = game:GetService("MarketPlaceService")
local success, Info = pcall(MPS.GetProductInfo, MPS, soundid)

if success then
    print("Exists")
else
    print("The sound doesnt exist or an HTTP error occured")
end
8 Likes

I just wanted to follow up on this because since ROBLOX started supplementing their own copyright-free music in place of songs that get claimed, this might not actually be true. It would technically be a valid music track, but the intended song would not play.

Here’s an imperfect solution:

If you want to get information about an asset, you might be able to determine if the name contains something to signal that it’s been deleted. We can use :GetProductInfo() and check if the title is “(Invalid Audio)”

local id = 123
local invalidNames = {
"(Invalid Audio)",
"[Content Deleted]",
}
local assetInfo = game:GetService("MarketplaceService"):GetProductInfo(id)
local valid = not table.find(invalidNames,assetInfo.Name)
if valid then
-- proceed!
end

The reason this is imperfect is because the uploader of the sound can still configure the asset name after it’s been deleted, but that rarely happens.

Hope this helped more

1 Like

Wrap it in a pcall, that might work

Didn’t mean to reply to a reply

You can probably do something with PreloadAsync here.

local ContentProvider = game:GetService("ContentProvider")

local function isSoundValid(soundId)
    local isValid = false

    local sound = Instance.new("Sound")
    sound.SoundId = "rbxassetid://" .. soundId

    ContentProvider:PreloadAsync({sound}, function(_, status)
        isValid = status == Enum.AssetFetchStatus.Success
    end)

    return isValid
end

if isSoundValid(0) then
    print("epic")
end

Here’s a way using Promises.

local function isSoundValid(soundId)
    local sound = Instance.new("Sound")
    sound.SoundId = "rbxassetid://" .. soundId

    local loadPromise = Promise.new(function (resolve)
        ContentProvider:PreloadAsync({sound}, resolve)
    end):andThen(function (contentId, status)
        if status == Enum.AssetFetchStatus.Success then
            return Promise.resolve()
        else
            return Promise.reject()
        end
    end)

    return loadPromise:awaitStatus() == Promise.Status.Resolved
end

if isSoundValid(0) then
    print("epic")
end
3 Likes

Usually its says removed to due Copyright violations if you search up the ID.

local marketplace = game:GetService("MarketplaceService")
local getProductInfo = marketplace.GetProductInfo

local function isAssetAudio(assetId : number) : boolean
	local success, result = pcall(getProductInfo, marketplace, assetId)
	if success then
		if result then
			if result.AssetTypeId == Enum.AssetType.Audio.Value then --Asset is an audio.
				return true
			else
				return false
			end
		end
	else
		warn(result)
	end
end

https://developer.roblox.com/en-us/api-reference/function/MarketplaceService/GetProductInfo