HUD Error

Hi, I’m kinda new to LUA coding but I wanted to get better at it, so I tried to create a simple armor/health bar hud for darkrp.
I get no errors but it ends up looking like this
Heres my HUD Code:


function hudPaint()
local ply = LocalPlayer()
local HP = ply:Health()
local Armor = ply:Armor()

--Background
draw.RoundedBox(4, 0, ScrH()-55, 1360, 60, Color(0, 0, 0, 212))

--HP/Armor Background
draw.RoundedBox(4, 260, ScrH()-44, 220, 40, Color(0, 0, 0, 226))
draw.RoundedBox(4, 21, ScrH()-45, 220, 40, Color(0, 0, 0, 226))

--HP/Armor
draw.RoundedBox(4, 260, ScrH()-44, math.Clamp( HP, 0, 200 )*2, 40, Color(4, 0, 255, 255))
draw.RoundedBox(4, 22, ScrH()-46, math.Clamp( Armor, 0, 200 )*2, 40, Color(255, 0, 0, 255))

--Text
draw.DrawText(ply:Health(), "Arial24", 131.5, 730, Color(255, 255, 255, 255))
draw.DrawText(ply:Armor(), "Arial24", 378.5, 728, Color(255, 255, 255, 255))
end
hook.Add("HUDPaint", "DarkRP_Mod_HUDPaint", hudPaint)

If you can fix this for me, please add me on steam here.

Change this

to this



draw.RoundedBox(4, 22, ScrH()-44, math.Clamp( HP, 0, 200 )*2, 40, Color(4, 0, 255, 255))
draw.RoundedBox(4, 260, ScrH()-46, math.Clamp( Armor, 0, 200 )*2, 40, Color(255, 0, 0, 255))


You had your Armor and HP bars reversed

I got the same problem, just the armor and HP bar are reversed.

Can someone please help me?

[editline]23rd February 2015[/editline]

Can someone atleast try and help? I’m pretty new to LUA and I don’t know why its not working right.

Rate me dumb, hate me or whatever but stop saying “LUA”

First, convert your health and armor values to a 0-1 value. Next, use the modifier you just created to multiply against the max-width of the bar, there’s that. So, the back-ground bar, if you use one, will be width max-width while the colored portion will be max-width * HealthModifier or ArmorModifier.

Proper HUD Creation: https://dl.dropboxusercontent.com/u/26074909/tutoring/hud/proper_hud_creation.lua.html

How to set up a HUD to scale properly: https://dl.dropboxusercontent.com/u/26074909/tutoring/hud/understanding_hardcoding_of_screensizes.lua.html

Some examples:

https://dl.dropboxusercontent.com/u/26074909/tutoring/hud/basic_healthbar.lua.html - Notice this one uses _w * _modifier; that’s what I talked about above… This way you isolate segments of the code to make it easier to modify later… Say you don’t like the width, modify the width and that’s it…

You’re hard-coding values instead of using simple tricks which makes it harder to change later on…

You could look into polys: https://dl.dropboxusercontent.com/u/26074909/tutoring/poly/creating_shapes_using_poly.lua.html

https://dl.dropboxusercontent.com/u/26074909/tutoring/poly/tilted_rectangle_poly_as_health_meter.lua.html - notice I create a helper function which simplifies the poly creation process by setting it so I only need to define x, y, w, h and tilt instead of awkward vars such as start x, start y, end x, end y, etc…

https://dl.dropboxusercontent.com/u/26074909/tutoring/poly/simplified_circles_with_poly.lua.html

If you do use poly, I’d suggest creating them once outside of the hook ( to prevent needing to create a new table each time it draws ) then drawing them in the hook… Update the table if the resolution changes.

Hope this helps!

You are passing HP to the armour bar and Armor to the health bar. It should be this:



--HP/Armor
draw.RoundedBox(4, 260, ScrH()-44, math.Clamp( Armor, 0, 200 )*2, 40, Color(4, 0, 255, 255))
draw.RoundedBox(4, 22, ScrH()-46, math.Clamp( HP, 0, 200 )*2, 40, Color(255, 0, 0, 255))


Acecool is right, though; you should follow his advice.

Sorry, I’m calling it by its name but yanno I guess I’m a noob for doing that

A lot of people get confused and think it’s an acronym. Lua is actually the Portuguese word for “Moon”, and is where the name for the language came from. :downs:

Thank you for this It worked.

[editline]24th February 2015[/editline]

Next time I make a HUD, I’ll most likely follow these instructions. Thanks alot for the help dude I really appreciate all of this!

Do you know where I could put images to use in my HUD? like where in the folders?

Stick them in gamemodes/your_gamemode/content/materials/your_server_or_gamemode_name/whatever_you_want/to/image.png

You can also use addons/your_addon_name/materials/…

If you want a system to make all of your resource files be downloaded by the client ( or at least resource.AddSingleFile each one so it can be downloaded from FastDL or ServerDL[ not recommended from ServerDL ] ) then take a look at this: https://dl.dropboxusercontent.com/u/26074909/tutoring/server_srcds_steamcmd/setting_up_downloads_using_recursive_resource_system.lua.html