Hi, I have been getting good with UserInputService on roblox. I want to know how to check if a player has pressed a key 2 times. Should I use some value and use if statements or is there an feature for it?
✓ ANSWER ACCEPTED
How do you check if a player has pressed key 2 times?
2 answers
✓ Accepted answer
That's really easy
local KeyPressed = 0
--Put UserInputService here
--Put Key Input Function here
KeyPressed = KeyPressed + 1
if KeyPressed >= 2 then
print("The Player has pressed the key more than 2 times")
end
-- LocalScript in StarterCharacterScripts or some other Local location
local inputService = game:GetService('UserInputService')
local keys = {
}
inputService.InputBegan:Connect(function(input)
-- turn the Enum into a string
local str = tostring(input.KeyCode)
-- is it in our table yet? Did we press that key yet?
local index = keys[str]
if index then
-- if we pressed it twice
if index >= 2 then
print(string.format("%s has been pressed %s times!",str,index))
end
else
-- else if it's not set, then se that key press to 1
keys[str] = 1
end
-- inc up by one on each press
keys[str] = keys[str] + 1
end)
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
Ok, thanks for the script.
Instead of posting a thank you comment. Accept my answer with the "Accept Answer" button below my answer.