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)
Well, if you are using RemoteEvent for firing the value, it will be performed as a separate function and not interrupt the function that is already being fired.
I edited your script and I guess the following script might work:
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Player, humanoid, keydown) if keydown then -- If keydown is true if game.ReplicatedStorage:FindFirstChild(tostring(Player.UserId)) then print("OK!") else local bool = Instance.new("BoolValue") -- Checks if the name of Bool value is BoolValue only bool.Parent = game.ReplicatedStorage bool.Name = tostring(Player.UserId) end end wait(0.1) -- Added wait, just for safety if the another statement gets checked before the Bool value is creates if keydown then -- If keydown is true if game.ReplicatedStorage[tostring(Player.UserId)].Value == false then game.ReplicatedStorage[tostring(Player.UserId)].Value = true -- Sets it to true else return -- If it's not true, stops the function of RemoteEvent end else game.ReplicatedStorage[tostring(Player.UserId)].Value = false -- If keydown is false, turns the value of bool value to false end while keydown do -- loops when the keydown value is true if game.ReplicatedStorage[tostring(Player.UserId)].Value == true then -- Checks if the value is true, i.e., if the key is pressed humanoid.Health = humanoid.Health - 1 -- Does what you wants else break -- If it's false, breaks the loop end wait(0.3) end -- [[ Removed useless lines from your code. The main idea is that when the key is pressed the bool value turns true, and if not, the value remains or becomes false. Don't change the values of bools using Local Script as it will only change locally and the script might continue and not break the loop or close the function. ]] -- [[ If you want to save the bool values anywhere else, then change the directory but don't mess with [tostring(Player.UserId)]. Here, I parented the bool value to the ReplicatedStorage but the most efficient way is to create a Folder and add the values of the bool in that Folder. ]] end)
Lemme know if it helps!