Hello I'm trying to make a script where when you hold it drains the persons health, although when you release the keybind it doesn't take health anymore.
The PROBLEM: The problem is, even after I let go of the key, it still drains health. Any help would be greatly appreciated.
Local Script:
local Player = game:GetService('Players').LocalPlayer
local UIS = game:GetService('UserInputService')
local Mouse = Player:GetMouse()
local Replicated = game:GetService('ReplicatedStorage')
UIS.InputBegan:Connect(function(input, gameProccessedEvent)
if gameProccessedEvent then return end
if input.KeyCode == Enum.KeyCode.C then
local hum = Mouse.Target.Parent:FindFirstChild('Humanoid')
if hum then
Replicated.RemoteEvent:FireServer(hum, true)
end
end
end)
UIS.InputEnded:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.KeyCode == Enum.KeyCode.C then
local hum = Mouse.Target.Parent:FindFirstChild('Humanoid')
Replicated.RemoteEvent:FireServer(hum, false)
end
end)
Server Script:
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Player, humanoid, keydown)
local chokeperson = false
if keydown then
chokeperson = true
while chokeperson == true do
humanoid.Health = humanoid.Health -1
wait(0.3)
end
end
if not keydown then
print('Key Released')
chokeperson = false
end
end)

7 comments
The while true do loop makes the code below stop until the loop is broken.
this code either 1. Allows only one person to choke at a time or 2. Allows the victim to unchoke themselves
They said they wanted to make a code that when you hold a button it drains your health. Read the description again.
No I-
So nobody has a clue?
The while true do loop makes the code below stop until the loop is broken. That is my best guess on what is happening here. Just try to make the if statement before the loop (or in the loop).
Maybe my answer may help