How can I check if health decreased in the last 0.7 seconds?

Hey! I’m trying to make a damage cancelling system (in which, if player has been damaged in the last 0.7 seconds, then they won’t be able to activate a tool).

I’ve been trying a lot of stuff for the past 2 hours, but nothing seems to work. I don’t want it to check EVERY 0.7s, but to check if the health was lower 0.7 seconds ago and fire a remote to server.
Tried using loops, tick(), .HealthChanged and a lot of things, but still can’t find out how to make this work. Do you have any ideas?

Thank you for reading, I’d really appreciate your help!

Hello, This might help you.

local v2 = true
game.Players.LocalPlayer.Character:WaitForChild("Humanoid").HealthChanged:Connect(function()
if v2 then
v2 = false
wait(0.7)
v2 = true
end
)

--connect this function with tool activation--
local function f1()
if not v2 then
print("Health Changed") --change this according to your needs
else
print("Health Not Changed")
end
end
1 Like

V1 is never used in your script but otherwise that’s the right approach.

ahh sorry, I guess I have messed up. Let me check that again

You could log the time of each health change and compare it with the current time;

local humanoid = humanoid

humanoid.HealthChanged:Connect(function()
    lastHealthChange = os.clock();
end)

while true do
    task.wait()
    if (os.clock() - lastHealthChange <= 0.7) then
        print("Health has changed within the last 0.7 seconds")
    end
end```
1 Like

You can have a health changed event to detect when the humanoid was damaged and keep track of the time it happened:

local Humanoid = --Path to humanoid
local Tool = --Path to tool
local Event = --Path to event you want to fire if the Humanoid was damaged within the last 0.7 seconds and the tool was activated

local PreviousHealth = Humanoid.Health
local LastDamaged = 0

Humanoid.HealthChanged:Connect(function()
   local Health = Humanoid.Health
   local Difference = Health - LastDamaged
   PreviousHealth = Health

   if Difference < 0 then --The humanoid's health was damaged
      LastDamaged = os.clock()
   end
end)

Tool.Activated:Connect(function()
   local Tick = os.clock()
   if Tick - LastDamaged < 0.7 then --The humanoid was damaged within the last 0.7 seconds

      Event:FireServer(...)

   else --The humanoid wasn't damaged within the last 0.7 seconds

       --Do stuff!

   end
end)

1 Like

Your code doesn’t take into account whether the Humanoid was healed instead of being damaged. Also, the post mentioned something about not wanting a loop.

1 Like

This will make me wait 0.7s. I want to already know if health was decreased in the PAST (previous to activation) 0.7s.

Thank you for replying!

I did something similar with tick(); but my damage detection local script is in StarterCharacterScripts, while my activation local script is in the tool itself. Also, I’ve never said anything about not wanting loops (I just said that I tried to used them).

Thank you for replying!

You could try using attributes for the damaged detection and then check it in the activation script.

Also, I was looking at this part:
image