A few things:
lines 3-7:
client = LocalPlayer()
DrawHealth = LocalPlayer():Health()
DrawArmor = LocalPlayer():Armor()
EchoHealth = LocalPlayer():Health()
EchoArmor = LocalPlayer():Armor()
You set a (global!) variable client to LocalPlayer(), and then never use it.
You have two separate values for LocalPlayer():Health() and LocalPlayer:Armor(). Why do you need two variables to store the
exact same value?
lines 9 - 13:
if DrawArmor < 0 then DrawArmor = 0 end
if DrawArmor > 100 then DrawArmor = 100 end
if DrawHealth < 0 then DrawHealth = 0 end
if DrawHealth > 100 then DrawHealth = 100 end
math.Clamplines 48-62:
local red = Color( 100, 0, 0 )
local curTime = 0
local stop = 380 -- ending value
local seconds = 5 -- number of seconds you want it to take
local function drawStuff()
curTime = curTime + RealFrameTime()
value = Lerp( curTime / seconds, start, stop )
surface.SetDrawColor( red )
surface.DrawRect( 20, ScrH() - 85, value, 28 )
end
Your variable 'red' is a constant, no need to define it every time the HUD is drawn. Move it outside of the Base function.
You're setting curTime to 0 every time the HUD is drawn. Your health bar will just stay at a few pixels because each frame you're resetting it. Move curTime outside of the Base function.
Your variables 'start', 'stop', and 'seconds', are all constant. Move them outside of the Base function.
You're missing the start variable. If you don't have it, you (should) get an error. Define start to be some number (you probably want it to be 0).
Of course, when you run your code you
don't get an error, because: you're never running the code within drawStuff. If you define a function but never run it, nothing will happen. Remove the function syntax and just put the code directly in Base.
value should be local.
After all these changes, we get:
local red = Color( 100, 0, 0 )
local curTime = 0
local start = 0 -- starting value
local stop = 380 -- ending value
local seconds = 5 -- number of seconds you want it to take
function Base()
-- rest of HUD
curTime = curTime + RealFrameTime()
local value = Lerp( curTime / seconds, start, stop )
surface.SetDrawColor( red )
surface.DrawRect( 20, ScrH() - 85, value, 28 )
end
Now, this is still not going to do what you want it to do. In my first post I said:
To implement this in your HUD, just store the old health (the health value from last frame) and lerp from it to the new health every frame."
You're just lerping from 0 to 380, and you're only doing it once. You need to:
- Store the health from the previous frame
- Check if the health has changed
- Lerp from the old health to the new health (only if the health has changed)
Good luck!