Mute button. HELP

Hello
I’m new to luau, about 1 week into self-learning.
I’m having a hard time figuring out what’s wrong with my mute script … feeling kinda stupid rn.
can someone please tell me where I went wrong and the reason behind it.
And if anyone has any tutorials I can watch to get better, please tell me.

Summary
local text = script.Parent.TextLabel
local button = script.Parent.ImageButton
local Play = false
local Music = game:GetService("SoundService"):WaitForChild("SoundGroup") 

local red = Color3.fromRGB(206, 5, 35)   -- #ce0523
local blue = Color3.fromRGB(0, 100, 206) -- #0064ce

button.MouseButton1Click:Connect(function()
	if not Play then
		Play = true
		text = "Unmute"
		button.Image = "rbxassetid://109569601328817"
		button.BackgroundColor3 = blue
		Music.Play = true
	elseif Play then
		Play = false
		text = "Mute"
		button.Image = "rbxassetid://99771432807793"
		button.BackgroundColor3 = red
		Music.Play = false
	end
end)

Its called luau, but i am unfamiliar with the sound service, why don’t you just do music:Start() and music:Stop() and instead of sound service, just have the music you’re playing as an instance.

If you want to mute all sounds,
you can do

for i, v in workspace:GetDescendants() do
   if v:IsA("Sound") then
      v.Volume = 0
   end
end
1 Like

Music.Play = true
and
Music.Play = false
Are not valid statements. :Play() is a method you can use on the sound instance, it is not a property you can change.

You can go 2 ways about fixing this, Either you replace Music.Play = true with Music:Play() and Music.Play = false with Music:Stop()

Or you can replace Music.Play = true with Music.Volume = 1 and Music.Play = false with Music.Volume = 0

4 Likes

please, dont do workspace:GetDescendants(). it is too expensive and is basically equal to trying to find a specific grain of sand in a desert.

person above me is right, but a bit wrong at the same time. you cannot call :Play() or set .Play on a SoundGroup.

you could actually make this look a lot better in my opinion, and also could potentially improve performance.

-- since you are a bit new to Luau, i'll be sure to format it.

local text = script.Parent.TextLabel
local button = script.Parent.ImageButton
local Play = false
local Music = game:GetService("SoundService"):WaitForChild("SoundGroup") 

local red = Color3.fromRGB(206, 5, 35) 
local blue = Color3.fromRGB(0, 100, 206) 

button.MouseButton1Click:Connect(function()
-- so, there are these magic things called bytecode! imagine this like an engine that runs a car. 
	Play = not Play -- Play is set to the opposite bool (true/false) of its current value. (true > false, false > true)
	text = Play and "Mute" or "Unmute" -- if Play is true, the text is Mute, otherwise it is Unmute
	button.Image = "rbxassetid://99771432807793"
	button.BackgroundColor3 = red
	Music.Play = false -- Music is a sound group. you cannot set .Play or call :Play() on a sound group. just remove this line.
end)

the operation Boolean = not Boolean is great for boolean toggles, and is perfect to use in a mute button or anything else you have to toggle.

there is one outlying problem with your code, and that is you cannot directly mute SoundGroups, as they are meant for mixing sound effects, not for sound containers. one approach to take is when playing a sound, put it in a folder in workspace, then iterate over it (although this would negate all performance measures you just made)

2 Likes
button.MouseButton1Click:Connect(function()
	if not Play then
		Play = true
		text = "Unmute"
		button.Image = "rbxassetid://109569601328817"
		button.BackgroundColor3 = blue
		Music:Play()
	elseif Play then
		Play = false
		text = "Mute"
		button.Image = "rbxassetid://99771432807793"
		button.BackgroundColor3 = red
		Music:Stop()
	end
end)

Whatever your sound is called, its Stop() and Play()

1 Like

Thats true but how could i do it otherwise, if the sound wasn’t ubder workspace l? I normally just have them as a child of the workspace, but theOP might not

1 Like

If you want to have a feature to mute a large number of sounds you should be using a SoundGroup instance and connecting all of those sounds to it and muting it from the SoundGroup instead.

2 Likes

I remember that I also had trouble with mute button scripting..
I can help you too:

local textLabel = script.Parent.TextLabel
local button = script.Parent.ImageButton
local Play = true 
local MusicGroup = game:GetService("SoundService"):WaitForChild("SoundGroup") 
local MuteVolume = 0.0 
local UnmuteVolume = 0.5 

local red = Color3.fromRGB(206, 5, 35)   
local blue = Color3.fromRGB(0, 100, 206) 

textLabel.Text = "Mute"
MusicGroup.Volume = UnmuteVolume
button.BackgroundColor3 = red

button.MouseButton1Click:Connect(function()
    Play = not Play

    if not Play then
        textLabel.Text = "Unmute"
        button.Image = "rbxassetid://99771432807793"
        button.BackgroundColor3 = blue
        MusicGroup.Volume = MuteVolume
    else
        textLabel.Text = "Mute"
        button.Image = "rbxassetid://109569601328817"
        button.BackgroundColor3 = red
        MusicGroup.Volume = UnmuteVolume
    end
end)

I hope it works for ya

1 Like

there is a property named “Volume” under SoundGroups. if you set it to 0, all sounds with the SoundGroup property set to that SoundGroup will inherit the volume.

2 Likes