Audio not Muffling when player presses Escape

Hey developers!

So I’m creating a system for my game for when the player presses Escape the currently playing audio muffles, and when they press escape again it unmuffles, and vice versa.

There is an Infinite Yield showing in the output window as shown below:

The script(s) aren’t showing any other errors, except for when I press Escape and the EqualizerSoundEffect doesn’t enable nor disable.

Here’s my code:

-- Server script 
local songs = {
	"rbxassetid://9522348460",
	"rbxassetid://9493377191",
	"rbxassetid://9522342391",
	"rbxassetid://13176013937",
}

local currentSongIndex = 1
local songPlayDelay = 5
local equalizerSettings = {LowGain = -2.6, MidGain = -46.7, HighGain = -46.7}

local function playNextSong()
	-- Play the current song
	local sound = Instance.new("Sound")
	sound.Name = "GameMusic"  -- Unique name to identify the sound
	local equalizer = Instance.new("EqualizerSoundEffect")
	equalizer.Name = "GameMusicEqualizer"
	equalizer.LowGain = -2.6
	equalizer.MidGain = -46.7
	equalizer.HighGain = -46.7
	equalizer.Enabled = false  -- Initially disabled
	equalizer.Parent = sound

	sound.SoundId = songs[currentSongIndex]
	sound.Parent = game.Workspace
	sound:Play()

	currentSongIndex = currentSongIndex + 1
	if currentSongIndex > #songs then
		currentSongIndex = 1
	end

	sound.Ended:Wait()

	wait(1)
	sound:Destroy()
end

local remoteEvent = Instance.new("RemoteEvent")
remoteEvent.Name = "ToggleEqualizer"
remoteEvent.Parent = game.ReplicatedStorage

remoteEvent.OnServerEvent:Connect(function(player)
	local character = player.Character
	if character then
		local sound = character:FindFirstChild("GameMusic")
		if sound then
			local equalizer = sound:FindFirstChild("GameMusicEqualizer")
			if equalizer then
				equalizer.Enabled = not equalizer.Enabled
				print("Equalizer toggled: " .. tostring(equalizer.Enabled))  -- Debugging print
			else
				print("Equalizer not found.")  -- Debugging print
			end
		else
			print("GameMusic not found.")  -- Debugging print
		end
	end
end)

local function startSongPlaylist()
	while true do
		playNextSong()
		wait(songPlayDelay)
	end
end

startSongPlaylist()
-- Client script (LocalScript)
local UserInputService = game:GetService("UserInputService")
local sound = nil 

-- Function to toggle the equalizer effect
local function toggleEqualizer()
	local remoteEvent = game.ReplicatedStorage.ToggleEqualizer
	remoteEvent:FireServer()
end

-- Event handler for key presses
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then return end
	if input.KeyCode == Enum.KeyCode.Escape then
		print("Escape key pressed.")  -- Debugging print
		toggleEqualizer()
	end
end)

I’ve tried adding print statements into the code too, but nothing is printing! If you can help me, please, let me know!

try removing “if gameprocessedevent then” maybe it triggers when you open the roblox window

Well, it printed! But I still got an infinite yield.

image
And its also in workspace too, kinda weird.

you need to specify the sound name, not the type

So would I specify it as?

local music = workspace:WaitForChild("GameMusic")

yeah

Thank you so much, it works! I appreciate it!

I also have another question. Since the music muffles and unmuffles when the player presses escape, I noticed that it doesn’t detect rapid key presses, so there’s always a little delay after each key press. Any reason for that?

I don’t know =(

Oh alright, is it because there’s a wait cooldown somewhere that I didn’t see?

it’s possible

Here’s a video example of what I mean (don’t mind the hideous avatar)


It’s supposed to unmuffle when the CoreGUI is no longer visible

i can view code for this?

Sure!

-- LocalScript

local UserInputService = game:GetService("UserInputService")
local currentMusic = nil

-- Function to find and update the current music
local function updateCurrentMusic()
	currentMusic = game.Workspace:FindFirstChild("GameMusic")
end

-- Function to toggle the equalizer effect
local function toggleEqualizer()
	if currentMusic then
		local equalizer = currentMusic:FindFirstChild("GameMusicEqualizer")
		if equalizer then
			equalizer.Enabled = not equalizer.Enabled
			print("Equalizer toggled: " .. tostring(equalizer.Enabled))  -- Debugging print
		else
			updateCurrentMusic()  -- Update the reference and try again
			if currentMusic then
				local equalizer = currentMusic:FindFirstChild("GameMusicEqualizer")
				if equalizer then
					equalizer.Enabled = not equalizer.Enabled
					print("Equalizer toggled after update: " .. tostring(equalizer.Enabled))  -- Debugging print
				end
			end
		end
	else
		updateCurrentMusic()  -- Update the reference if no music is currently playing
	end
end

-- Event handler for key presses
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.Escape then
		print("Escape key pressed.")  -- Debugging print
		toggleEqualizer()
	end
end)

-- Initial update of the current music reference
updateCurrentMusic()
-- ServerScript

local songs = {
	"rbxassetid://9522348460",
	"rbxassetid://9493377191",
	"rbxassetid://9522342391",
	"rbxassetid://13176013937",
}

local currentSongIndex = 1
local songPlayDelay = 5
local equalizerSettings = {LowGain = -2.6, MidGain = -46.7, HighGain = -46.7}

local function playNextSong()
	-- Play the current song
	local sound = Instance.new("Sound")
	sound.Name = "GameMusic"  -- Unique name to identify the sound
	local equalizer = Instance.new("EqualizerSoundEffect")
	equalizer.Name = "GameMusicEqualizer"
	equalizer.LowGain = -2.6
	equalizer.MidGain = -46.7
	equalizer.HighGain = -46.7
	equalizer.Enabled = false  -- Initially disabled
	equalizer.Parent = sound

	sound.SoundId = songs[currentSongIndex]
	sound.Parent = game.Workspace
	sound:Play()

	currentSongIndex = currentSongIndex + 1
	if currentSongIndex > #songs then
		currentSongIndex = 1
	end

	sound.Ended:Wait()

	wait(0.1)
	sound:Destroy()
end

local remoteEvent = Instance.new("RemoteEvent")
remoteEvent.Name = "ToggleEqualizer"
remoteEvent.Parent = game.ReplicatedStorage

remoteEvent.OnServerEvent:Connect(function(player)
	local character = player.Character
	if character then
		local sound = character:FindFirstChild("GameMusic")
		if sound then
			local equalizer = sound:FindFirstChild("GameMusicEqualizer")
			if equalizer then
				equalizer.Enabled = not equalizer.Enabled
				print("Equalizer toggled: " .. tostring(equalizer.Enabled))  -- Debugging print
			else
				print("Equalizer not found.")  -- Debugging print
			end
		else
			print("GameMusic not found.")  -- Debugging print
		end
	end
end)

local function startSongPlaylist()
	while true do
		playNextSong()
		wait(songPlayDelay)
	end
end

startSongPlaylist()

hmm… I don’t know…
You see dalays between print actions?

Only after I press it the first time, for example:

I press escape once, it prints. I press it again, it doesn’t print, but when I press it again, it prints

I think it’s roblox problem
try use task.spawn() on function for new stream. I don’t know :frowning:

Basically, it doesn’t print subsequently.

I’ll try using that, since I’m still a little new to programming in lua, it’s for the local script, right?