Would this work for Making a Health bar Disapear within a Period of time?

Hello, So I am wondering about making a Health bar Disappear overtime, so when the Player takes Damage, The Health Bar would Appear when the Player has Taken Damage by something after doing a check to make sure they have and then Disappear if the Player hasn’t taken Damage within a Period of Time, But I was wondering if this Example would actually work for what I’m trying to do?

Note that I didnt look at anything to help with, I made all inside this code block, so If I made any mistakes, please let know, and If there is a way I can improve this code, that would be very helpful.

local DelayTime       = 5  -- Time to Check to hide UI again
local IsHidden        = true -- Gui would be Hidden
local TimeHit         = nil -- If Player is hit (nil for the start)
local LastHp          = Humanoid.Health -- Last Health (Should be >= 100)

Humanoid.HealthChanged:Connect(function(Hp)
    if Hp < LastHp then -- If the Player Took Damage
        print"Damaged"
        IsHidden = false -- Gui would Appear
        TimeHit = os.time() -- Gets the Time The Player Took Damage
    end
    LastHp = Hp -- Updates LastHp to be more Recent
end)

-- Can I simplify this code below?
task.defer(function()
    while true do
        task.wait(1) -- yields for one Second
        if TimeHit and (os.time() - TimeHit) >= DelayTime then -- if The Player was hit and Time greater than or equal to 5
            IsHidden = true -- Hides Gui again
        end
    end
end)
1 Like
local tickreal = tick()
Humanoid.HealthChanged:Connect(function(HP)
   tickreal = tick()
   task.delay(DelayTime, function()
       if Humanoid.Health == HP then
          IsHidden = true
       end
   end)
end)
  

This would work with anything that is a number.

1 Like

Isn’t tick() deprecated?

Not really, it is still used widespread. You can use time() if you wanna.
" * tick() sounds perfect - it has a high resolution (usually around 1 microsecond), and a well-defined baseline - it counts since UNIX epoch! Or, well, it actually doesn’t. On Windows, it returns you a variant of the UNIX timestamp in local time zone. In addition, it can be off by 1 second from the actual, real UNIX timestamp, and might have other idiosyncrasies on non-Windows platforms. We’re going to deprecate this in the future."

Still not deprecated.

2 Likes

Also about this, this wouldn’t be exactly what I want, I just want it to disappear when it is over a period of time when they take Damage, not when the Max Health is actually reached, but thanks tho.

im pretty sure time() is just when the Server started running, but that could work

1 Like

It would compare if the changed health is equal to LastHp if they’re equal then it is hidden. I use this method to detect if an IntValue hasn’t changed in a specific time if it didn’t slowly, decrease it.

2 Likes

maybe its better if it use os.clock() or time() and Humanoid:GetPropertyChangedSignal("Health")

1 Like

GetPropertyChangedSignal is unnecessary. As there is already a connection for health changes.

1 Like

Yeah, I Understand that, But It isnt exactly what I want though, It is useful and I thank you for that, but Just wondering if the code I put would actually work for that I’m trying to do?

Sure, you can try.

Yeah, So I modified it a bit so it doesnt keep repeating, and used time() instead of os.time(), it appears to be working, so I guess thats done, I wont mark my own post as the Solution tho.

2 Likes

I’m going to bump this as I’m wondering if this code can be improved on more?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.