Detect when a keybind is being held and when hold stops

What do you want to achieve?

I want to detect when a keybind is held, and when it stops being held.
While the keybind is being held, I want the player to take damage and play an animation, I also want an int value to decrease on the player.

Eg: Hold H, player health decreases by 5 every second, intvalue inside player decreases by 2 every .5 seconds. damage stops and value stops decreasing after H is not being held anymore.

i’m aware I would need a remote event for this to pick it up on the server, but I’m struggling on how I would detect when a keybind is not being held and is being held.

1 Like

localscript in startercharacterscripts:

(i dont have much as I need help with a lot of stuff)
a lot of this is probably wrong

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local event = game.ReplicatedStorage.Hold

key = key:lower()
if key == 'h' then

1 Like

Try this out:

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input, processed)
	if processed then
	if input.KeyCode == Enum.KeyCode.E then
		local clock = os.clock()
			warn("inputted key")
		until UIS:IsKeyDown(Enum.KeyCode.E) == false
		warn("held key for " .. os.clock() - clock .. " seconds!")
local Game = game
local RunService = Game:GetService("RunService")
local UserInputService = Game:GetService("UserInputService")

local function OnInputBegan(StartInput, GameProcessed)
	local Connection, StartTime = nil, RunService.Stepped:Wait()
	local function OnInputEnded(EndInput, GameProcessed)
		if StartInput.KeyCode ~= EndInput.KeyCode then return end
		local EndTime = RunService.Stepped:Wait()
		print(string.format("%s key held for %0.2f seconds.", EndInput.KeyCode.Name, EndTime - StartTime))

	if GameProcessed then return end
	Connection = UserInputService.InputEnded:Connect(OnInputEnded)



How would I stop something when the keybind is released?
for example, the target takes damage while the keybind is held and the damage stops when the keybind is released.