Help with making a button that stops or starts playing audio from several speakers in my game

I am making a system that you can press one button and the audios from three speakers currently will stop or start playing. The problem is, I have no idea how to do this.

Basically, I have a GUI with a button, and I need this button to start music from three different speakers,
and keep in mind that the Sound components are in the speakers. The script is in the Frame that my button is in.

If you haven’t already, you can put the speakers in a Folder or group them into a Model. If you don’t want to do that, make sure you’ve named your speakers a unique name, like “Speaker”, then you can loop through the workspace and find the Instances named Speaker.

Here’s an example using the Folder/Model method:

-- * Replace 'Button' with the button's name.
script.Parent.Button.Activated:Connect(function()
    -- * Replace 'speakers' with the location of your model or folder.
    for _, speaker in speakers:GetChildren() do
        local audio = speaker.Sound -- * Replace 'Sound' with the audio's name.
        
        if audio.IsPlaying then
            audio:Stop()

        else
            audio:Play()
        end
    end
end)

Here’s an example using the Name method:

script.Parent.Button.Activated:Connect(function()
    for _, descendant in workspace:GetDescendants() do
        if not descendant:IsA("Sound") or descendant.Parent.Name ~= "Speaker" then
            continue
        end

        if descendant.IsPlaying then
            descendant:Stop()

        else
            descendant:Play()
        end
    end
end)

Remember to use a LocalScript since graphical user interface (GUI) input is client-sided.

Thank you so much for putting your time into this! You truly are a lifesaver! The system works perfectly.

Have a great day!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.