How to constantly update descendants without loop?

hi! i want to make a muffled/underwater effect whenever the player goes to the core roblox menu.
i wrote this script myself that scans through all sound objects then add and tween the EQ.
however, my game constantly creates new sound meanwhile (eg. footsteps) and that would not be registered afterwards.

how do i make it such that it constantly checks for new sounds and add in the descendants?

any help would be appreciated! thanks :slight_smile:

-- muffled sound active
guiservice.MenuOpened:Connect(function()
	for _, v in pairs(game:GetDescendants()) do
		if v:IsA("Sound") then
			local eq = Instance.new("EqualizerSoundEffect")
			eq.Parent = v
			eq.Name = "EQ"
			if eq then
				local tween = ts:Create(eq, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {HighGain = -80, MidGain = -40, LowGain = 1})
                tween:Play()
			end
		end
	end
end)
-- muffled sound gone
guiservice.MenuClosed:Connect(function()
	for _, v in pairs(game:GetDescendants()) do
		if v:IsA("Sound") then
			local eq = v:FindFirstChild("EQ")
			if eq then
				local tween = ts:Create(eq, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {HighGain = 1, MidGain = 1, LowGain = 1})
                tween:Play()
				tween.Completed:Connect(function()
					eq:Destroy()
				end)
			end
		end
	end
end)

It should be registering, try printing out v after it has checked it. Also another thing to note is that it doesn’t check to see if the sound already has an equalizer effect, so it makes duplicate sounds every time you open and close the menu, try this:

-- muffled sound active
guiservice.MenuOpened:Connect(function()
	for _, v in pairs(game:GetDescendants()) do
		if v:IsA("Sound") and not v:FindFirstChild("EqualizerSoundEffect") then
            print(v.Name)
			local eq = Instance.new("EqualizerSoundEffect")
			eq.Parent = v
			eq.Name = "EQ"
			if eq then
				local tween = ts:Create(eq, TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {HighGain = -80, MidGain = -40, LowGain = 1})
                tween:Play()
			end
		end
	end
end)

Theres a chance that it may not be receiving it at all, since the loop is called every time the menu is activated

that’s what i’ve been trying to figure out :sob:
it would just muffle the pre-existing sound but new sounds would be applied that EQ effect

Could be that the equalizers are overlapping to make it seem stronger, you can try adjusting the values in the original equalizer once it has been made once.

they aren’t overlapping…? idt u grasp my issue, hmmm would u like me to re-explain?

you could try using DescendantAdded event

This would be really bad as it would add a connection to every single sound in the game, and would take a performance toll on the game.

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