Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to cancel wait() after a value is changed?

Asked by 2 years ago

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)

2 answers

Log in to vote
0
Answered by 2 years ago

An easy way to solve this would be just putting the Health -= 13 above the wait(1)

Ad
Log in to vote
0
Answered by
0x5y 105
2 years ago

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.

0
This does not fix the problem, it still damages me after the grabbed value is false. Uniplier 83 — 2y

Answer this question