How to check whether player hasn't taken any damage within 5s?

I’m in need of assistance. How do you know whether the player hasn’t taken any damage in the last five seconds? and if that player takes damage again within the 5s it’ll have to wait another 5 seconds for the script to do something,.

This script works fine but it continues even though the player has taken damage within 5s, it doesn’t wait again for 5s. I’m not sure if this explanation made sense but hopefully you got some of it.

-- Variables are already added, just didn't include it in this script.
humanoid.HealthChanged:Connect(function(CurrentHealth)
    wait(5)
    print("Test")
end
2 Likes

Something like this can be achieved VIA a loop:

local timer = 5;
local lastHp = humanoid.Health;
coroutine.wrap(function()
    while humanoid.Health > 0 do -- loop until the player dies
        repeat
            wait(1);
            timer -= 1;
        until timer <= 0;
        -- what you want to do after 5 seconds of no damage
        timer = 5;
    end
end)() -- create a coroutine and start it immediately

humanoid.HealthChanged:Connect(function(CurrentHealth)
    if(currentHealth < lastHp) then -- they took damage
        timer = 5; -- reset timer
    else
        lastHp = currentHealth; -- they healed, set new scope
    end
end
3 Likes

Thank you for this, but how do you make the loop run again if the player has been damaged, the loop seems to stop if the timer has reached 0

I’ve added a loop that will continue until the player dies. Please check it now.

Did you post the same question in ScriptingHelpers?

Thank you so so so much, I’ve been struggling for a few hours on how do I do this, I’m not that good at scripting.

Yeah, I sort of did then I thought the website was down since it was showing me a message that it’s offline. I should probably delete it

Well, you might want to post a link to this thread there, so someone in the future can see the solution.

1 Like

BTW yall dont have to do ; he only did it because he is use to doing it because of a different language :slight_smile:

1 Like