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

