How to check how long a player held a key

I THINK it may be tick() or some sort but I need to check if a player holds E for 10 seconds. Is it possible, and any tips on how to?
You don’t need to write the entire script, just give me a way how or a document.

1 Like

Just start a custom made timer whenever InputBegan gets fired from UserInputService

You can make timers with while true do’s or repeat untils

2 Likes

psuedo code would be like

allotedTime = 0
past = os.clock()
event: keypressed {
  past = os.clock()
}
event: keyreleased {
  allotedTime = os.clock() - past
}
2 Likes
local UserInputService = game:GetService("UserInputService")
local HoldTime = 0
UserInputService.InputBegan:Connect(function(input)
   if input.UserInputType == Enum.UserInputType.Keyboard then
      HoldTime = tick()
   end
end)

UserInputService.InputEnded:Connect(function(input)
   if input.UserInputType == Enum.UserInputType.Keyboard then
      HoldTime = tick() - HoldTime
      print(HoldTime)
   end
end)
3 Likes

Seems to work, but when i change it to only E, it doesn’t work at all (Nothings works in the function)

UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.KeyCode.E then
		HoldTime = tick()
	end
end)
1 Like

You should check a keycode, not UserInputType.

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		HoldTime = tick()
	end
end)
1 Like