How do I make a GUI that mutes and unmutes the background music?

I want to make the music stop playing when a player presses the mute music button.

I can’t figure out the mute part of the script.

I have tried looking at tutorials etc. but none helped with my issue.

This is my current code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AudioPlayer = require(ReplicatedStorage:WaitForChild("AudioPlayer"))
local startergui = game:GetService("StarterGui")
local mute = startergui.menu.mute
local unmute = startergui.menu.unmute

AudioPlayer.setupAudio({
	["Elevator"] = 1845375096,
})

AudioPlayer.playAudio("Elevator")

function mutepress()
	
end

function unmutepress()
	AudioPlayer.playAudio("Elevator")
end

mute.Activated:Connect(mutepress)
unmute.Activated:Connect(unmutepress)

I am stuck on the

function mutepress()
end

bit as I am not sure what I would put in it.

2 Likes

How about changing the volume? When you want to mute it set the volume to 0, and then when unmuting set it to the old volume.

Also what is this “audio player”

Please show us the code for the AudioPlayer module if possible.

Just set the volume for the music to be 0 then the volume back to the default volume if they enable music again.

One of your first issues is that you’re trying to access the buttons through StarterGUI. This won’t work as you will have to access the PlayerGUI in order to detect button activations.

Assuming that the music is going to be parented to SoundService, you may be able to run a simple loop for all the songs in there and mute them. I would keep the music player in a different script and have the mute and unmute button script in a local script in your ScreenGUI as so:

image

The localscript within the ScreenGUI is what actually has access to the mute and unmute. Try this and let me know if this works, also make sure its in the same place I have put it:

local MuteButton = script.Parent.Menu.Mute
local UnmuteButton = script.Parent.Menu.Unmute

MuteButton.Activated:Connect(function()
	for i,v in pairs(game:GetService("SoundService"):GetChildren()) do
		if v.ClassName == "Sound" then
			v.Volume = 0 -- Mutes the volume
		end
	end
end)

UnmuteButton.Activated:Connect(function()
	for i,v in pairs(game:GetService("SoundService"):GetChildren()) do
		if v.ClassName == "Sound" then
			v.Volume = 0.5 -- Change to desired volume
		end
	end
end)