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.
local UserInputService=game:GetService('UserInputService')
local KeyTarget=Enum.KeyCode.H -- The H Key
local Held,Loop=Instance.new('BoolValue')
local ClockA,ClockB=os.clock(),os.clock()
local function HalfSecond()
-- Decrease Int Value inside Player
end
local function FiveSecond()
-- Decrease Health of Player
end
UserInputService.InputBegan:Connect(function(Input,GameProcessed)
if GameProcessed then return end
if Input.KeyCode==KeyTarget then
if Held.Value==true then return end
Held.Value=true
if not Loop then
Loop=game:GetService('RunService').RenderStepped:Connect(function()
local Current=os.clock()
if (Current-ClockA)>=0.5 then
HalfSecond()
ClockA=os.clock()
end
if (Current-ClockB)>=5 then
FiveSecond()
ClockB=os.clock()
end
end)
end
end
end)
UserInputService.InputEnded:Connect(function(Input,GameProcessed)
if GameProcessed then return end
if Input.KeyCode==KeyTarget then
if Held.Value==false then return end
Held.Value=false
if Loop then Loop:Disconnect() Loop=nil end
end
end)