Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you check if a player has pressed key 2 times?

Asked by 6 years ago

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?

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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
0
Ok, thanks for the script. activatesenju 2 — 6y
0
Instead of posting a thank you comment. Accept my answer with the "Accept Answer" button below my answer. magicguy78942 238 — 6y
Ad
Log in to vote
2
Answered by
Azarth 3141 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago
-- 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)

Answer this question