How would I find the speed of pressing a button

Hello, I want to calculate how fast the player is pressing a button on their keyboard, for example lets use the key “R”

UserInput.InputBegan:Connect(function(Input, gameProcessed)
	if not gameProcessed then
		if Input.KeyCode == Enum.KeyCode.R then
			--Do something
		end
	end
end)

I tried doing something with the tick() / time() function but I just couldn’t find the right method.

If the Player doesn’t press the key for a certain amount of time, the speed will be 0, if they spam the button a LOT it could be like 4 or 6

Are you trying to find the average speed that they’re clicking the button?

Speed = distance/time, but in this case the “distance” would be how many times you’ve clicked and well… time is time.

local speed = 0
local totalTime
local totalClicks = 0
local startTime = 0
UIS.InputBegan:Connect(function(input, gp)
    if gp then return end
    if input.KeyCode == Enum.KeyCode.R then
        if totalClicks == 0 then
            startTime = tick()
        end
        totalClicks = totalClicks + 1
    end
end)

while true do
    if startTime > 0 then
        totalTime = tick() - startTime
        speed = totalClicks/totalTime
    end
    wait(0.5)
end
1 Like

What’s your specific use case? Do you explicitly need how quickly a user presses a button or are you just looking to make a double press feature?

If it’s the latter, you simply need to check the tick difference between the previous press and a current click.

If it’s the former, there’s somewhat of a lengthy process to that. In addition to resetting press number after a certain time of inactivity, you’ll have to track time presses yourself. Time comes from the tick difference, number of clicks comes from each tap and speed comes from dividing the number by the time.