Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I fix this audio script?

I am trying to make it so when a user inputs a sound ID in the textbox and presses the play button it will play the desired song. However I can't find out what's wrong. I have tried ..tonumber(input.Text) but that does not seem to work.

local Input = script.Parent.Parent.Parent.MusicID

function play()
    for i, v in pairs(game.Workspace:children()) do if v:IsA("Sound") then v:Destroy() end end	
	local s = Instance.new("Sound", game.Workspace) s.SoundId = "http://www.roblox.com/asset/?id=" ..tonumber(input.Text)
	s.Volume = 1 
	s.Pitch = pitch 
    s.Looped = true 
    s.archivable = false repeat 
	s:Play() 
wait(2.5) 
	s:Stop()
wait(.5) 
 	s:Play() until 
	s.IsPlaying
end


script.Parent.MouseButton1Down:connect(play)


EDIT:

local Input = script.Parent.Parent.Parent.MusicID

function play()
   for i, v in pairs(game.Workspace:GetChildren()) do
	end if v:IsA("Sound") then 
	v:Destroy() end end 
local s = Instance.new("Sound", game.Workspace)
	s.SoundId = "http://www.roblox.com/asset/?id=" ..(Input.Text)
	s.Volume = 1 
	s.Pitch = pitch 
    s.Looped = true 
    s.archivable = false repeat 
	s:Play() 
wait(2.5) 
	s:Stop()
wait(.5) 
 	s:Play() until 
	s.IsPlaying
	




script.Parent.MouseButton1Down:connect(play)

2 answers

✓ Accepted answer

The only problem that I notice is that you have:

local s = Instance.new("Sound", game.Workspace) s.SoundId = "http://www.roblox.com/asset/?id=" ..tonumber(input.Text)

But you just need to make it like this:

local s = Instance.new("Sound", game.Workspace)
s.SoundId = "http://www.roblox.com/asset/?id=" ..(Input.Text)

You don't need to make it a number, since they are two strings going together, I think it would work even with the :tonumber() if you had 'input' capitalized like when you named the variable, the variable is 'Input'. but you put 'input' in the :tonumber() arguments.

Nvm, I also noticed this so just replace line 4 with this:

 for i, v in pairs(game.Workspace:GetChildren()) do
 if v:IsA("Sound") then

Whole script fixed:

local Input = script.Parent.Parent.Parent.MusicID

function play()
   for i, v in pairs(game.Workspace:GetChildren()) do
 if v:IsA("Sound") then 
    v:Destroy() 
end
end
local s = Instance.new("Sound", game.Workspace)
    s.SoundId = "http://www.roblox.com/asset/?id=" ..(Input.Text)
    s.Volume = 1 
    s.Pitch = pitch 
    s.Looped = true 
    s.archivable = false 
repeat 
    s:Play() 
wait(2.5) 
    s:Stop()
wait(.5) 
    s:Play() until 
    s.IsPlaying
end





script.Parent.MouseButton1Down:connect(play)

6 comments

0

This solution did not work either. :( Check my edit in the question to see what I changed.

0

Did you replace both line 4 and line 5?

0

Yes.

0

I will repost the whole script for you.

0

Okay.

0

Wait, when I press the play button, the button that plays the song you inputed, nothing happens? I checked the parents and everything.

tonumber() would actually break the script. That first line, instead of doing a bunch of parent, why not just use game.Players.LocalPlayer.PlayerGui and go from there?

With that many .Parent's it is very easy to get confused.

This discussion is preserved as it was. New answers, comments, and votes are closed.