Slow moving health bar

Hello
I can’t quite work out how I would make a health bar that slowly moves down to the health you have. For example you have 100hp and you fall, you are now on 50hp. How would I make it so that it would take a second or two to move down to 50hp? I know I have to use offsets but I don’t quite understand how to use it.

[lua]draw.RoundedBox(0, ScrW()*0.5, ScrH()*0.5, math.Clamp(ply:Health()*2, 0, 200), 30, Color(30,30,30,255))[/lua]

Instead of using ply:Health() to display the health, use a variable. use math.TimeFraction to create a 0-1 modifier over a course of time to change lower or raise the variable to the new amount over time.

This can be done entirely clientside using a game-event listener.

[lua]
//
// This is a basic template for creating game_event listeners/hooks with the purpose of including all
// arguments / table variables for each game-event to make it easily known which values can be accessed
// and when: player_hurt. player_hurt is triggered when the player is injured or dies.
//
// SHARED-REALM – Both the CLIENT and SERVER can receive this hook.
// The SERVER receives 1 extra value :: Priority.
//
gameevent.Listen(“player_hurt”)
hook.Add( “player_hurt”, “player_hurt:Example”, function( _data )
local _health = _data.health; // Remaining health after injury
local _priority = ( SERVER ) && _data.Priority || 5; // Priority ??
local _id = _data.userid; // Same as Player:UniqueID( );
local _attackerid = _data.attacker; // Same as Player:UniqueID( ); but it’s the attacker id.

// Called when the player is injured or dies.

end );[/lua]

It shows the remaining health after the injury, so on Spawn you’ll need to set the variable to 100 also with game-event listeners.

[lua]//
// This is a basic template for creating game_event listeners/hooks with the purpose of including all
// arguments / table variables for each game-event to make it easily known which values can be accessed
// and when: player_spawn. player_spawn is triggered when the player initially spawns, or respawns.
//
// SHARED-REALM – Both the CLIENT and SERVER can receive this hook.
//
gameevent.Listen(“player_spawn”)
hook.Add( “player_spawn”, “player_spawn:Example”, function( _data )
local _id = _data.userid; // Same as Player:UniqueID( );

// Called when the player spawns initially or respawns.

end );[/lua]

Restructuring tutorials, so some links may be broken, but I listed the two you’d need, on top of HUDPaint hook for the actual drawing.
https://dl.dropboxusercontent.com/u/26074909/tutoring/game_event_listeners.lua

[editline][/editline]
If you can’t get TimeFraction to work for you, you can also use CurTime( ) - StartTime to get the change. Divide that by how many seconds you want it to take, and clamp from 0-1; same thing as fraction / float…

-snip- No edit line…

While informative, that’s extremely overcomplicated for what the OP wants.




--Outside your hud paint function
local LastHealth = 0
local TimeToMove = 1 --Seconds
local MaxHealth = 100

--Inside
LastHealth = math.Approach( LastHealth, LocalPlayer( ):Health( ), ( RealFrameTime( ) * MaxHealth ) / TimeToMove )

local f = math.Clamp( LastHealth / MaxHealth, 0, 1 )

draw.RoundedBox( x, y, w * f, h, color )

Fantastic. One of my issues is I over-complicate some things, while other things I simplify well. Great post!

As Kogistune said, thanks for all the great information but it was a lot more complicated and I don’t really understand any of it. Thanks Kogitsune for simplifying it :slight_smile: