How do I make all of the sounds mute/unmute?

I have a loading screen, and when it’s loading I want all of the sounds to be turned off to make it seem more like a loading screen. I don’t want the sounds to stop, I just want them to set their volume to 0 temporarily. How can I do that?

3 Likes

You’ll also have to add a DescendantAdded or something along the lines of that to mute new sounds.

1 Like

Making this script was harder than I thought. This is my latest version. You can read what I did and why in the script comments.

local allSounds = {} -- Store sounds in table

local function resetVolume(sound)
	if sound.Volume ~= 0 then -- Do not save volume when it is set to 0
		allSounds[sound][1] = sound.Volume -- Save volume to index 1 of table
		sound.Volume = 0
	end
end

local function checkInstance(object)
	pcall(function()
		if object:IsA("Sound") then -- Detect if sound
			allSounds[object] = {} -- Create table for sound
			resetVolume(object)
			allSounds[object][2] = object:GetPropertyChangedSignal("Volume"):Connect(function()
				resetVolume(object) -- Reset sound when the volume is changed
			end)
		end
	end)
end

for _, object in ipairs(game:GetDescendants()) do -- Get every sound in the game
	checkInstance(object)
end
local soundAdded = game.DescendantAdded:Connect(checkInstance) -- Reset volume of sound added to game

task.wait(4) -- This is where you put your loading code stuff

soundAdded:Disconnect() -- Cleanup
for object, array in next, allSounds do
	array[2]:Disconnect()
	object.Volume = array[1] -- Reset volume to original
end
table.clear(allSounds)

I used :GetDescendants() to get every sound in the game. Note how I switch from using ipairs to next (which is the same thing as pairs by the way). This is because :GetDescendants() returns a table with proper indexes ([1] = part, [2] = part, [3] = part) whereas my table saved the sound as an index (sound = {}, sound = {}, sound = {}).

Edit: Here is a more simple (and arguably more efficient) version of the script:

local allSounds = {}
local connections = {}

local function resetVolume(sound)
	if sound.Volume ~= 0 then
		allSounds[sound] = sound.Volume
		sound.Volume = 0
	end
end

local function checkInstance(object)
	pcall(function()
		if object:IsA("Sound") then
			resetVolume(object)
			table.insert(connections, object:GetPropertyChangedSignal("Volume"):Connect(function()
				resetVolume(object)
			end))
		end
	end)
end

for _, object in ipairs(game:GetDescendants()) do
	checkInstance(object)
end
table.insert(connections, game.DescendantAdded:Connect(checkInstance))

-- Run your code here

for _, connection in ipairs(connections) do
	connection:Disconnect()
end
table.clear(connections)
for object, volume in next, allSounds do
	object.Volume = volume
end
table.clear(allSounds)
4 Likes

Thanks! Sorry it took me so long to reply.
One thing though, is that the script spams a bunch of errors called " The current identity (2) cannot Class security check (lacking permission 3)"

It printed out the error where you put this:

if object:IsA("Sound") then

end

I don’t know how to fix it though.

edit: I just wrapped it in a pcall function and it worked. Thanks!

Honestly though, the easiest, quickest and most stable way of muting all sounds is to simply use a SoundGroup object. Assign all sounds to one new SoundGroup and then set the volume of the SoundGroup to 0 when the loading screen appears. That will mute all sounds, but they will still be playing.

2 Likes

Well I tried using sound groups before and I couldn’t get it to work. Also all of the sounds in my game have a different volume and they are in different models.

Update: Moved the pcall function to the correct place.

Different volumes won’t matter if the soundgroup is at volume 0. Any number multiplied by zero becomes zero.
Try looking into sound groups again, it’s a very important/useful feature for audio development

3 Likes

It matters when you want to unmute the sounds

1 Like