Gui to play music not working

In this game you put the SoundID in the TextBox and press a button to listen to music, it won’t let me play the song like I want, here’s the code:

Try using Instance | Roblox Creator Documentation

local Music = script.Parent:WaitForChild('Sound')
local MusicId = Music.SoundId
local Button = script.Parent:WaitForChild('TextButton')
local Input = script.Parent:WaitForChild('TextBox')

Button.MouseButton1Click:Connect(function()
    Music.SoundId = Input.Text
    Music.Playing = true
end)
1 Like

I tried the code but it didn’t work.

Is there an error in the output?

1 Like

Yes

Failed to load sound (SoundID): Unable to download sound data

Try the :Play() function on the music.

2 Likes

Do you know how to use that to find a SoundID from a text box?

First of all, #help-and-feedback:code-review is for WORKING code, this is more appropiate in #help-and-feedback:scripting-support. Secondly, you’re setting an ID as the url to the sound. What you do, is that you concatenate the protocol rbxassetid:// and the id together, you need to replace the line that sets the ID to Music.SoundId = "rbxassetid://" .. Input.Text, this should resolve back to the proper URL and hopefully, your music plays. It’s also a good practice to use :Play() instead of .Playing since it’s more common to use a function that sets the state of something rather than setting it yourself.

2 Likes

Thanks, this is good advice, but when I tried to put the ID 5950454613 into the TextBox then pressed the play button nothing happend, what my game is you put a SoundID in the TextBox then you press a play button, after this the song from the SoundID will play, here are some screenshots:

Right, this is because you cached the SoundId into a variable. This means that you kept a COPY of the SoundId, not a reference. To change the reference (and, the actual music), you do Music.SoundId = existing code here.

1 Like

Try this:

local Music = script.Parent:WaitForChild('Sound')
local Button = script.Parent:WaitForChild('TextButton')
local Input = script.Parent:WaitForChild('TextBox')

Button.MouseButton1Click:Connect(function()
    local ID = tonumber(Input.Text)
	if ID then
		local Success,Info = pcall(function()
	    	return game.MarketplaceService:GetProductInfo(ID)
    	end)
	    if Success and Info and Info.AssetTypeId == 3 then
    		Music.SoundId = "rbxassetid://" .. ID
			if not Music.IsLoaded then
			    Music.Loaded:Wait()
		    end
	    	Music:Play()
    	end
	end
end)
1 Like

It’s way too complex for me but I’ll try it.

The code worked, however the code is too complex for me.

Small explanation of each part:

Make sure everything is numbers.

Make sure it is a valid id for a sound.

Load and wait for the sound to be ready to play.

1 Like

Please keep in mind everyone is different, so when asking for help, expect complex answers. You can always ask for an explanation

1 Like

Here’s the game: Test your music ID's! - Roblox