HUD Creation Problems

Hi there,

It’s my first try giving Lua a go so i decided to do something i hoped to be simple, but once i got a working health bar going i realized that it was a little small, so i tried doing a calculation:


function MGSHud()

    local health = LocalPlayer():Health()
    
    surface.SetDrawColor( 120, 120, 255, 50 )
    surface.DrawRect( 40, 40, 350, 20 )
    
    surface.SetDrawColor( 100, 255, 100, 180 )
    surface.DrawRect( 40, 40, Health * 3.5, 20 )
    
end 
hook.Add( "HUDPaint", "MGSLife", MGSHud )

Somehow, i managed to screw up real bad and now a health bar no longer displays, and when ever i go into Garry’s Mod with it on i get this Lua error:


Hook 'MGSLife' Failed: [addons\mgshud\lua\autorun\client\mgshud.lua:9] attempt to perform arithmetic on global 'Health' (a nil value)

Does anyone know what im doing wrong?

Thanks

Well for some reason your health is buggered up and it reads health as nil. Im still learning, but is the health variable buggered up somewhere, it says the error is on line 9, so post that up


	surface.DrawRect( 40, 40, Health * 3.5, 20 )

That’s Line 9

You’re trying to get “Health”, yet the var is “health”.

Oh God, Thanks so much, I am a complete imbecile.

Just one last question

To refresh the hud, since I am editing it while still playing, do i need to disconnect and start another single player game or do i need to restart Gmod for Hud changes to take effect?

Go in to garrysmod/garrysmod/lua place your file in here. Go in singleplayer and type lua_openscript_cl “filename should popup”

This only works without scriptenforcer enabled hope this helped! Aswell use [.lua][./lua] tags.

Thanks A lot!

A lot easier to edit it.

Hell ya :smiley:

also if you are editing a gamemode do gamemode_reload in console. Not the most reliable though.