UserInputService is firing twice everytime I press a key

I’ve used UserInputService before lots of times and I don’t understand why it’s firing twice on me. I’ve looked on other forums, about this exact problem, but I’ve already included debounces and made sure that the script was written right. I’ve finally decided to ask everyone else about this. To quickly recap what my problem is, whenever I press any key on the keyboard, the UserInputService will call it twice.

local userInputService = game:GetService("UserInputService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local player = game:GetService("Players").LocalPlayer

local remoteEventsFolder = replicatedStorage.RE
local angelInputEvent = remoteEventsFolder:WaitForChild("AngelInputEvent")

local mouse = player:GetMouse()
local firstPowerUse = true

userInputService.InputBegan:Connect(function(input, gameProcessed)
	
	if gameProcessed then return end
		
	if input.KeyCode == Enum.KeyCode.One then
		
		if firstPowerUse == true then
			
			firstPowerUse = false
			print("One key was pressed")
			--angelInputEvent:FireServer("One", mouse.Hit.Position)
			wait(2)
			firstPowerUse = true
			
		end
		
	end

end)

Am I missing something? I have a debounce already set up and I don’t think it’s because I’m firing a remote event either because I commented that line out when I was testing out the print line. I’m very confused.

maybe ur keyboard is just buggy? didnt happen to me before

I think you need a cooldown aka a debounce. If yo know UserInput service then I am sure you know how to add one of these.

I know you have something like this, but you need at the top

I already have one implemented in. I called it firstPowerUse instead of debounce.

then its either a studio bug or ur keyboards going crazy

I think you should add it before the if statements

I am assuming you are not holding the key for over 2 seconds

I’ll try that

-Got to have more characters. Don’t mind this

ok. If that does not work then I may agree with @keremMCT

Your method worked. I can’t believe I made a topic over that. Thank you for the help!

You need to use a :Disconnect() function. Try this:

local connection = nil

function onOneDown(input, gameProcessed)
    if gameProcessed then return end
		
	if input.KeyCode == Enum.KeyCode.One then
		
		if firstPowerUse == true then
			
			firstPowerUse = false
			print("One key was pressed")
                        connection:Disconnect()
			--angelInputEvent:FireServer("One", mouse.Hit.Position)
			wait(2)
                        connection = userInputService.InputBegan:Connect(onOneDown)
			firstPowerUse = true
			
		end
		
	end
end

userInputService.InputBegan:Connect(onOneDown)

when the event is fired, it disconnects the function when it activates, then it reconnects it when done.

glad I could help!
(extra characters)

1 Like

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