How to make a sound mute when a descendant is added

Basically what I’m trying to point out is if a new sound is made, how do I make it so that new sound’s volume is 0, then even if that sound is still there or removed, once the sfx is unmuted, the sound goes back to its original volume separately.

Script:

local music = game:GetService("SoundService"):WaitForChild("Music")
local volumeworkspace = {}
local volumegui = {}
local clicked = false



function onClicked()
	script.Parent["clickfast.wav"]:Play()
	clicked = not clicked
	if clicked == true then
		script.Parent.Text = "Unmute SFX"
		script.Parent.BackgroundColor3 = Color3.fromRGB(0,255,0)

		for _, obj in pairs(workspace:GetDescendants()) do
			if obj and obj:IsA("Sound") then
				volumeworkspace[obj] = obj.Volume
				obj.Volume = 0
			end
		end
		for _, obj in pairs(game.Players.LocalPlayer.PlayerGui:GetDescendants()) do
			if obj and obj:IsA("Sound") then
				volumegui[obj] = obj.Volume
				obj.Volume = 0
			end
		end
	elseif clicked == false then
		script.Parent.Text = "Mute SFX"
		script.Parent.BackgroundColor3 = Color3.fromRGB(255, 0, 0)

		for _, obj in pairs(workspace:GetDescendants()) do
			if obj and obj:IsA("Sound") then
				obj.Volume = volumeworkspace[obj]
			end
		end
		for _, obj in pairs(game.Players.LocalPlayer.PlayerGui:GetDescendants()) do
			if obj and obj:IsA("Sound") then
				obj.Volume = volumegui[obj]
			end
		end
	end
end

script.Parent.MouseButton1Click:Connect(onClicked)

You don’t need to do all of that, use SoundGroups to control the volume of a bunch of sounds instead! Simply create the group, and set Sound.SoundGroup to that group. Now the sound will react to the group’s volume and sound effects. Each group also reacts to ancestor groups.

What if a descendant was added, how would I make that sound muted?

When a descendant is added, set the SoundGroup to the appropiate group, such as one called “Sounds”! Also consider iterating existing descendants so you don’t miss any. From then on you can control all of those audios’ volume level by adjusting the SoundGroup’s volume.

I like using regular sounds instead of soundgroups when it comes to things like this. Because especially with tool sounds, I want a set volume, not just one volume set for all tool sounds.

Basically what I’m trying to point out is if a new sound is made, how do I make it so that new sound’s volume is 0, then even if that sound is still there or removed, once the sfx is unmuted, the sound goes back to its original volume separately.

sound:SetAttribute("Volume", sound:GetAttribute("Volume") or sound.Volume)
sound.Volume = 0

This code will save the original volume and then set the volume to zero. You can use this attribute to bring the volume back to it’s original state later.

sound.Volume = sound:GetAttribute("Volume")