I have this script that drains health when a player's "Grabbed" value is set to true, and to stop draining health when the value is false.
The problem is, after the value turns false, if it is currently waiting to hurt the character again, it will still hurt the character after the grabbed value is false. Is there any way to stop this?
char.charactervalues.Grabbed.Changed:Connect(function() if char.charactervalues.Grabbed.Value == true then while true do if char.charactervalues.Grabbed.Value == false then break; end wait(1) char.Humanoid.Health -= 13 end end end)
An easy way to solve this would be just putting the Health -= 13 above the wait(1)
When using a while (condition) do
loop, you should always pay attention to the condition part of it. For example, you can change your code to the following:
char.charactervalues.Grabbed.Changed:Connect(function() while char.charactervalues.Grabbed.Value and wait(1) do -- only runs if the player is currently grabbed, and has waited 1 second char.Humanoid.Health -= 13 end end)
This will wait 1 second before the player's health is diminished, but as soon as the player isn't grabbed anymore, the loop stops. If you don't want to wait 1 second before the health is diminished, you can add char.Humanoid.Health -= 13
before the while loop.