Mute music script not working

I’ll go a little more in depth with the title. This script has no problem with muting, but does when I try to unmute it.

local a = game.SoundService.BackgroundMusic
local b = game.SoundService["BackgroundMusic#2"]
local c = game.SoundService["BackgroundMusic#3"]
local d = game.SoundService["BackgroundMusic#4"]
local e = game.SoundService["BackgroundMusic#5"]
local f = game.SoundService["BackgroundMusic#6"]
local button = script.Parent
local MusicPlayer = workspace.MusicPlayer.MusicPlayer

	if a or b or c or d or e or f.Volume == .5 or .2 or 2.2 then
		a.Volume = 0
		b.Volume = 0
		c.Volume = 0
		d.Volume = 0
		e.Volume = 0
		f.Volume = 0
		button.Text = "Music: Disabled"
		button.BackgroundColor ="Really red")
		a.Volume = .5
		b.Volume = 0.2
		c.Volume = 0.5
		d.Volume = 2.2
		e.Volume = 0.5
		f.Volume = 0.5
		button.Text = "Music: Enabled"
		button.BackgroundColor ="Sea green")

(I know this script is very sloppy, I haven’t learned tables yet, but when I do, I’ll edit this script.)

Screenshot of script muting background music:


Is it supposed to be like this for play a couple of sounds at once? I just don’t understand a little

This script is for muting and unmuting all of the music in SoundService.
I check the Volume of all the audios in the if statement, and if it is the default value, then I make the Volume to 0.

However, it doesn’t unmute the audio, which is why I made this topic in the first place.

Well here’s the problem, you can’t do that kind of check or f.Volume == .5 or .2 or 2.2 then, you need either or f.Volume == .5 or f.Volume == .2 or f.Volume == 2.2 then or or table.find({0.5, 0.2, 2.2}, f.Volume) then



a.SoundId = --the id you use
--repeat with other sounds

This does not work, to visualize it the script will read it like so:

a ~= nil or b ~= nil or c ~= nil or d ~= nil or e ~= nil or f.Volume == .5 or .2 ~= nil or 2.2 ~= nil`

Instead I suggest the following

  1. Put all the songs into one dictonary.
  2. Loop through the arrays and check their volume.
  3. mute/set them again.
local SS = game:GetService("SoundService")
local sounds = {
   [SS["BackgroundMusic"]] = .5, 
   [SS["BackgroundMusic#2"]] = .2,
   [SS["BackgroundMusic#3"]] = .5,
   [SS["BackgroundMusic#4"]] = 2.2,
   [SS["BackgroundMusic#5"]] = .5,
   [SS["BackgroundMusic#6"]] = .5,

Now loop through them:

local isEnabled = true -- music is enabled by default
local debounce = false -- cooldown variable

local function toggleBGSFX(bool)
   for sound, maxSound in pairs(sounds) do
      sound.Volume = (bool and maxSound) or 0

   if debounce then return end
   debounce = true
   toggleBGSFX(not isEnabled)
   isEnabled = not isEnabled -- toggle state
   task.wait(1) -- cooldown time
   debounce = false
1 Like

Again, I would do this if I knew how to use tables, but thanks for the example.

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