Confused on Some Code

So, I’m trying to advance my proficiency in Lua, and I figured I’d see how to make a HUD. I knew the basic draw.RoundedBox shit, or the ol’ vgui.Create( “D[DEL]ick[/DEL]Frame” ). But I wanted to further my knowledge of vgui. Now, I watched the following video:


At around 4 minutes in, you can see that he’s making it so the health bar will not extend it’s capacity/vise versa.

He does the following:

	local DrawHealth = LocalPlayer():Health()
	if DrawHealth > 100 then DrawHealth = 100 end
	if DrawHealth < 0 then DrawHealth = 0 end
	if DrawHealth != 0 then
		draw.RoundedBox( 0, 17, ScrH() - 143, 326 * DrawHealth / 100, 26, Color( 255, 0, 0, 255 ) )

Now… My question is:

Why the hell does it work?! Would this not make your health actually go down to 100, if you increase it to… Say 200?

I mean… I think I know, however, I’m learning, so I’d like to confirm… This is clientside… The server handles your health. That’s why I think this works… Is this true?

that does not set health you know that right? I could be reading this wrong

He does this: 326 * DrawHealth / 100
So when your health is 100, 100/100 = 1. 326 is the maximum lenght of the bar.

This part:

if DrawHealth > 100 then DrawHealth = 100 end
if DrawHealth < 0 then DrawHealth = 0 end

Is just a clamp. If drawhealth goes over 100, it will clamp at 100. As XxLMM13xXx said, DrawHealth is a copy of the real health, so changing that variable won’t affect the player itself, only the bar.

Ohhhhhh so basically LocalPlayer():Health() is just the value of my health at that time? then when my hp is set to higher or lower than 100/0… it changes to 100/0?? Not my health, just that data stored in the variable?

Yeah. It can be shortened to DrawHealth = math.Clamp( DrawHealth, 0, 100 ), which is a base utility function.

Don’t mind me asking, because as I said in the thread I’m pretty new. What does math.Clamp do, exactly?

You don’t even need a variable.

draw.RoundedBox( 0, 17, ScrH() - 143, 326 *  math.Clamp( LocalPlayer():Health(), 0, 100 ) / 100, 26, Color( 255, 0, 0, 255 ) )

However, if you use the player health more than once in that block of code, it’s recommended to use a variable.

math.Clamp returns the value of the variable inside the first parameter, but if it is lower than the second parameter or higher than the third parameter, it returns the limits instead.

So, my final code is as follows(obviously doing more, but just seeing if this makes perfect sense to me):

local function Base() -- Base function

	draw.RoundedBox( 0, 5, ScrH() - 155, 350, 150, Color( 40, 40, 40, 250 ) ) -- Drawing boxes
	draw.RoundedBox( 0, 15, ScrH() - 145, 330, 30, Color( 100, 100, 100, 255 ) ) -- Drawing boxes
	local DrawHealth = LocalPlayer():Health() -- Storing players CURRENT health in a variable
	-- local EchoHealth = LocalPlayer():Health() -- I've yet to see wtf this nigga got multiple variables consisting of the same thing on here for
	draw.RoundedBox( 0, 17, ScrH() - 143, 326 * ( math.Clamp( DrawHealth, 0, 100 ) / 100) , 26, Color( 255, 0, 0, 255 ) ) -- Draws a box, and clinching it from going over/under 100/0

the comments are all explaining correctly, right?

Yes, and really, you can substitute in LocalPlayer():Health() in for DrawHealth and not have a variable at all. Reasoning: a variable’s primary purpose is to store data used multiple times or in multiple places. You only use it once, so it’s unnecessary.

[editline]17th February 2016[/editline]

As for the EchoHealth, it looks like it was just left over from old code – useless now.

I think you should change

( math.Clamp( DrawHealth, 0, 100 ) / 100 )


( math.Clamp( LocalPlayer():Health(), 0, LocalPlayer():GetMaxHealth() ) / LocalPlayer():GetMaxHealth() )

Just in case

Well, I didn’t know GetMaxHealth() was a thing. Now that I do, I will change it to that. Thanks a bunch bro, for telling me about that!

[editline]18th February 2016[/editline]

I see, I will do that. I really don’t see a reason not too, lol.