Why when I press two keys at the same time that activate sound effects, only one plays?

Hey, I am creating a drum set that when you click specific keys, the drum set will play a certain sound. Currently I have a bug where if you click two keys at the same time, sometimes only one sound will be played even though the console prints both as being played.

My hypothetical on why this is happening is it has to do with something with the hi-hat toggle I made. Hi-hats in real life are connected to a pedal, when you press down on said pedal the hi-hat closes and makes a shorter sound than if it was open. To make this toggle, I have a variable that toggles from true or false if holding down “Z”. If you are holding down “Z” and attempt to play a hi-hat (“J”), it will play the closed hi-hat sound.

The problem only occurs when trying to play a closed hi-hat with any other instrument at relatively the same time and I can usually cause the problem to happen if I do a flam (when you hit the drums with both hands at about the same time but one hand is delayed)

The code also is two scripts, a client script inside of StarterPlayerScripts that tells a server script within a seat to play a sound within the server using RemoteEvents

Code within StarterPlayerScripts

-- Player Info
local player = game:GetService("Players").LocalPlayer
local character = player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

-- Drum Info
local inputService = game:GetService("UserInputService")
local drumSounds = game.ReplicatedStorage.Drums
local state = true
pedal = false

-- Checks if player is on the correct seat
humanoid:GetPropertyChangedSignal("SeatPart"):Connect(function()
	print("Function restart, current state: ", state)
	if humanoid.SeatPart and humanoid.SeatPart.Name == "DrumSeat" then
		state = not state
		print("Player getting on seat, toggling state: ", state)
	else
		state = not state
		print("Player exiting seat, toggling state: ", state)
	end
	
end)

-- Checks if player is pressing a playing key
inputService.InputBegan:Connect(function(input)
	if not state then
	
		if pedal == false and input.KeyCode == Enum.KeyCode.J then
			drumSounds.OpenHi:FireServer()
		end
		
		if pedal == true and input.KeyCode == Enum.KeyCode.J then
			drumSounds.CloseHi:FireServer()
		end
		
		if input.KeyCode == Enum.KeyCode.K then
			drumSounds.Ride:FireServer()
		end
		
		if input.KeyCode == Enum.KeyCode.C then
			drumSounds.Tom1:FireServer()
		end
		
		if input.KeyCode == Enum.KeyCode.N then
			drumSounds.Tom2:FireServer()
		end
		
		if input.KeyCode == Enum.KeyCode.L then
			drumSounds.TomF:FireServer()
		end
		
		if input.KeyCode == Enum.KeyCode.A then
			drumSounds.Snare:FireServer()
		end
		
		if input.KeyCode == Enum.KeyCode.Space then
			drumSounds.Kick:FireServer()
		end
		
	end
end)


-- Checks if player is holding down the pedal
inputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Z then
		pedal = true
		print(pedal)
	end
end)

-- Checks if player is holding down the pedal
inputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Z then
		pedal = false
		print(pedal)
	end
end)

Server script within the drum seat

-- Sound effects
local sound = game.SoundService.Drums.SoundLocation
local sounds = { 
	-- Cymabls
	sound.openHi, --1
	sound.closedHi, --2
	sound.ride, --3

	--toms
	sound.tom1, --4
	sound.tom2, --5
	sound.floorTom, --6

	--other
	sound.snare, --7
	sound.kick, --8
}

local drumSounds = game.ReplicatedStorage.Drums


drumSounds.CloseHi.OnServerEvent:Connect(function()
	sounds[2]:Play()
	print("Playing Closed Hi-Hat")
end)

drumSounds.OpenHi.OnServerEvent:Connect(function()
	sounds[1]:Play()
	print("Playing Open Hi-Hat")
end)

drumSounds.Ride.OnServerEvent:Connect(function()
	sounds[3]:Play()
	print("Playing Ride")
end)

drumSounds.Tom1.OnServerEvent:Connect(function()
	sounds[4]:Play()
	print("Playing Tom1")
end)

drumSounds.Tom2.OnServerEvent:Connect(function()
	sounds[5]:Play()
	print("Playing Tom2")
end)

drumSounds.TomF.OnServerEvent:Connect(function()
	sounds[6]:Play()
	print("Playing Floor Tom")
end)

drumSounds.Snare.OnServerEvent:Connect(function()
	sounds[7]:Play()
	print("Playing Snare")
end)

drumSounds.Kick.OnServerEvent:Connect(function()
	sounds[8]:Play()
	print("Playing Kick")
end)

RemoteEvents

Have you tried cloning the sound and play the cloned sound then delete after the sound stopped playing?

This happens because you’re calling :Play() on the same Sound instance every time. A Sound can’t play over itself, so when two keys fire close together, the second one interrupts the first. That’s why the prints show both, but you only hear one.

For drum kits you need polyphony. Instead of reusing the same Sound, clone it when you play it:

local s = sounds[2]:Clone()
s.Parent = sounds[2].Parent
s:Play()
game.Debris:AddItem(s, s.TimeLength)

That way each hit is its own sound instance, and they won’t cut each other off.

hope this helps :slight_smile:

I have not, what would be the best way of going through with this?

I tried doing this but not only does it seem like a bad way to do it.. it also just doesnt work :p

Please refer to this comment, this is the correct way