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

How to stop all audio from playing?

So, I have a list of songs, and when you click on their button, their respecting song plays. How can I use a stop button to stop audio from playing WITHOUT wasting tons of code space?

function onButtonClicked()
	script.Parent.Parent.Audio1:Stop()
	script.Parent.Parent.Status.Text = "Stopped"
end
script.Parent.MouseButton1Click:connect(onButtonClicked)

You can ignore the text part. As you can see, I only have one audio, but once the library becomes bigger...

function onButtonClicked()
	script.Parent.Parent.Audio1:Stop()
	script.Parent.Parent.Audio2:Stop()
	script.Parent.Parent.Audio3:Stop()
	-- AND SO ON...
	script.Parent.Parent.Status.Text = "Stopped"
end
script.Parent.MouseButton1Click:connect(onButtonClicked)

How can I achieve this same effect without wasting code space? If there is an alternate way to stop the audio currently playing, please tell me. Thanks!

2 answers

✓ Accepted answer

This only works if all the sounds are in something and there are nothing but sounds. You can add a line or two to check if it's a sound if you want.

function onButtonClicked()
    for k, v in pairs(script.Parent.Parent:GetChildren() do
    	v:Stop()
    end
    script.Parent.Parent.Status.Text = "Stopped"
end
script.Parent.MouseButton1Click:connect(onButtonClicked)

2 comments

0

Thanks! Also, a follow-up question: how can I make it so that people would have to stop the song (via the Stop button) before playing another song (via clicking a song button)?

0

Nevermind, I figured it out.

Post the whole script.

1 comment

0

That was the entire script for the Stop button. It's supposed to stop the music currently playing. I'm not sure if there is anything of relevancy in the other scripts for the song buttons (which just play music when clicked on).

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