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.
(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
mouse.KeyDown:Connect(function(key)
key = key:lower()
if key == 'h' then
event:FireServer(mouse.Target)
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input, processed)
if processed then
return
end
if input.KeyCode == Enum.KeyCode.E then
local clock = os.clock()
repeat
warn("inputted key")
task.wait()
until UIS:IsKeyDown(Enum.KeyCode.E) == false
warn("held key for " .. os.clock() - clock .. " seconds!")
end
end)
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
Connection:Disconnect()
local EndTime = RunService.Stepped:Wait()
print(string.format("%s key held for %0.2f seconds.", EndInput.KeyCode.Name, EndTime - StartTime))
end
if GameProcessed then return end
Connection = UserInputService.InputEnded:Connect(OnInputEnded)
end
UserInputService.InputBegan:Connect(OnInputBegan)
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.