Sound name script doesn't work

Hello! I was writing script which puts in text label current sound’s name. But wheck I laugh test game, output says there is an error. This is script I wrote:

local sound = game.Workspace.Sound
local SoundId = tonumber(sound.SoundId)
local Asset = game:GetService("MarketplaceService"):GetProductInfo(SoundId)

while wait() do
   script.Parent.Text = Asset.Name
end

And here is a error which wrote output:

output

How to fix that and make script work correct?

Are you sure that SoundId is not a nil value?

1 Like

The issue is you are doing tonumber() on a string that contains more than just numbers. If you want to get just the numbers you have to use Pattern Matching. Below is the post that solves this issue and this Developer Hub article that goes over strings explains how tonumber() works.

2 Likes

Maybe doing this would solve the problem?

local Sound = game.Workspace.Sound
local SoundId = Sound.SoundId:match("%d+")
local Asset = game:GetService("MarketplaceService"):GetProductInfo(SoundId)

while wait() do
    script.Parent.Text = Asset.Name
end

Just like what @xZylter said, you have to use pattern matching to get just the numbers and thats what I did or alternatively, you don’t have to do what I said but instead you can change tonumber(sound.SoundId) to tonumber([The ID of the sound, only numbers])

2 Likes

I tried that, but script does the same error for some reason

Your sound ID is most likely nil then, try grabbing your id from the Roblox website and see if its nil.

1 Like