Music Player Inefficient

Hello DevForum,

I’m trying to make a music player.

This is my current code:

local MusicFrame = script.Parent.Music
local IDBox = MusicFrame.IDBox
local PlayButton = MusicFrame.PlayButton

local Music = workspace.Music.Music

PlayButton.MouseButton1Click:Connect(function()
	Music.SoundId = "rbxassetid://"..IDBox.Text
	Music:Play()
end)

Script Info: the “Music” Variable is a sound object in workspace
IDBox is a Textbox Object.
PlayButton is a TextButton Object.

Well sure, this does work. So what’s the problem?

The problem is that I believe this is a very inefficient way of making a music player because if the player types any letters into the TextBox then it will error, preventing it from working, same if they input an ID that doesn’t exist.

You can check if an input is a number by using

if tonumber(str) then
--is a number
else
--is not a number
end

To check if an audio exists, you can use GetProductInfo on the asset id in a pcall.

local success, info = pcall(MarketplaceService.GetProductInfo, MarketplaceService, assetid)
if success then
-- is a valid id
end