BindAction Always Enables Even If Set To A Specific Keybind

Hello Scripters.

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.

1 Like

can you send a code snippet?​​​​​​​​

1 Like

ill give you the entire code

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)
1 Like

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)
1 Like

it works but when i just hover my mouse back to the client it just spawns unremovable sounds

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.

this is the script i have so far. let me know if anything causes this glitch

local cas = game:GetService("ContextActionService")
local uis = game:GetService("UserInputService")
local function IkeyPressed(actionName, inputState, inputObject)
	if inputState == Enum.UserInputState.Begin then
		print('I key 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()
			pcall(function()
				newsound:Destroy()
			end)
		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.R then
		newsound.PlaybackSpeed = 0.94
		newsound:Play()
		newsound.Ended:Connect(function()
			pcall(function()
				newsound:Destroy()
			end)
		end)
		elseif 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)

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)

The :BindAction replaces the need for the InputBegan. Is there a reason why you’re using both (for ‘I’ and the other KeyCodes)?

This:

Is equivalent to this:

uis.InputBegan:Connect(function(inp, gpe)
    if (inp.KeyCode == Enum.KeyCode.I) then
        IkeyPressed()
    end
end)

that worked pretty well.

like alot more than i thought

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.