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!
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?
-- 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()