Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ 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

2 comments

0

Ok, thanks for the script.

0

Instead of posting a thank you comment. Accept my answer with the "Accept Answer" button below my answer.

-- 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.