Unfortunately. I Don’t Know How To Show Video Proof In The Forum.
But When I Set A BindAction To SPECIFICALLY Make It Only Activate When The I Key Is Pressed.
It Dosen’t Work And Just Activate Alot Of Times Even When I’m Not Pressing The I Button.
Can Someone Help.
local cas = game:GetService("ContextActionService")
local uis = game:GetService("UserInputService")
local function IkeyPressed()
uis.InputBegan:Connect(function(inp, gpe)
if gpe then return end
print("I Pressed")
local newsound = Instance.new("Sound", game.SoundService)
newsound.SoundId = game.SoundService["piano note"].SoundId
newsound.PlaybackSpeed = 1.33
newsound:Play()
newsound.Ended:Connect(function()
newsound:Destroy()
end)
end)
end
uis.InputBegan:Connect(function(inp, gpe)
if not workspace.Part.Seat.Occupant then return end
if gpe then return end
local newsound = Instance.new("Sound", game.SoundService)
newsound.SoundId = game.SoundService["piano note"].SoundId
if inp.KeyCode == Enum.KeyCode.T then
newsound.PlaybackSpeed = 1
newsound:Play()
newsound.Ended:Connect(function()
pcall(function()
newsound:Destroy()
end)
end)
elseif inp.KeyCode == Enum.KeyCode.Y then
newsound.PlaybackSpeed = 1.12
newsound:Play()
newsound.Ended:Connect(function()
pcall(function()
newsound:Destroy()
end)
end)
elseif inp.KeyCode == Enum.KeyCode.U then
newsound.PlaybackSpeed = 1.23
newsound:Play()
newsound.Ended:Connect(function()
pcall(function()
newsound:Destroy()
end)
end)
elseif inp.KeyCode == Enum.KeyCode.I then
--cas:BindAction("IKey", IkeyPressed, false, Enum.KeyCode.I)
end
end)
From what I can see, when someone hits the ‘I’ key, it (intends to) run the IkeyPressed function. In this function you have an InputBegan event that will inevitably be duplicated every time the ‘I’ key is pressed. You don’t actually check within this event-function whether the I key is being pressed, so every key press will trigger the creation of a new sound. I believe simply removing the uis.InputBegan from the ikeyPressed function should fix most of your problems. Also the purpose of a BindAction is that you don’t need to check whether the I key is being pressed in an InputBegan prior:
local function IkeyPressed(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
print('I key pressed')
-- Make sound
end
end
cas:BindAction("IKey", IkeyPressed, false, Enum.KeyCode.I)
That would be this part. It will get run every time any sort of Input begins (mouse movement, clicks, key presses etc.). The simple solution is to make the sound from within the if inp.KeyCode == .... conditionals.
As I already explained, it’s the local newsound = part just after you bind the I key. Move it into the conditionals:
local cas = game:GetService("ContextActionService")
local uis = game:GetService("UserInputService")
local function makeSound()
local newsound = Instance.new("Sound", game.SoundService)
newsound.SoundId = game.SoundService["piano note"].SoundId
newsound.Ended:Connect(function()
pcall(function()
newsound:Destroy()
end)
end)
return newsound;
end
local function IkeyPressed(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
local newsound = makeSound()
newsound.PlaybackSpeed = 1.33
newsound:Play()
end
end
uis.InputBegan:Connect(function(inp, gpe)
if not workspace.Part.Seat.Occupant then return end
if gpe then return end
if inp.KeyCode == Enum.KeyCode.R then
local newsound = makeSound()
newsound.PlaybackSpeed = 0.94
newsound:Play()
elseif inp.KeyCode == Enum.KeyCode.T then
local newsound = makeSound()
newsound.PlaybackSpeed = 1
newsound:Play()
elseif inp.KeyCode == Enum.KeyCode.Y then
local newsound = makeSound()
newsound.PlaybackSpeed = 1.12
newsound:Play()
elseif inp.KeyCode == Enum.KeyCode.U then
local newsound = makeSound()
newsound.PlaybackSpeed = 1.23
newsound:Play()
end
end)
cas:BindAction("IKey", IkeyPressed, false, Enum.KeyCode.I)