I am trying to make a sliding health bar for a darkrp/starwarsrp server and I am having an issue with players loading in and then it causing an error because the player is invalid.
local function healthBar()
local plyHealth = LocalPlayer():GetMaxHealth()
smoothHealth = Lerp( 5 * FrameTime(), plyHealth, LocalPlayer():Health() )
draw.RoundedBox( 0, 19, ScrH() - 303, 86, smoothHealth *286 / 100, Color( 200, 50, 50, 255 ))
draw.RoundedBox( 0, 19, ScrH() - 183, 86, 56, Color( 0, 0, 0, 200 ))
surface.SetDrawColor( Color( 200, 200, 200, 255 ) )
surface.DrawOutlinedRect( 15, ScrH() - 180, 94, 50 )
end
That is my current code and all it does is make the health bar jitter as soon as they go below their max health, I have also tried:
local smoothHealth = LocalPlayer():GetMaxHealth()
local function healthBar()
smoothHealth = Lerp( 5 * FrameTime(), smoothHealth, LocalPlayer():Health() )
draw.RoundedBox( 0, 19, ScrH() - 303, 86, smoothHealth *286 / 100, Color( 200, 50, 50, 255 ))
draw.RoundedBox( 0, 19, ScrH() - 183, 86, 56, Color( 0, 0, 0, 200 ))
surface.SetDrawColor( Color( 200, 200, 200, 255 ) )
surface.DrawOutlinedRect( 15, ScrH() - 180, 94, 50 )
end
the second one works but only if the player is loaded in and isn't invalid as it gives me the error:[ERROR] addons/darkrpmodification/lua/darkrp_modules/tcb_hud_1/cl_main.lua:15: Tried to use a NULL entity!
1. GetMaxHealth - [C]:-1
2. unknown - addons/darkrpmodification/lua/darkrp_modules/tcb_hud_1/cl_main.lua:15
3. doInclude - [C]:-1
4. loadModules - gamemodes/darkrp/gamemode/libraries/modificationloader.lua:102
5. Call - gamemodes/darkrp/gamemode/libraries/modificationloader.lua:147
6. unknown - gamemodes/darkrp/gamemode/cl_init.lua:56
Thats to do with the fact the player isn't valid yet as they haven't finished loading so it can't actually get their max health and since it's starwarsrp and health changes due to job I can't just set it to a static value. I want to get the players max health and have smoothHealth be that value so then the Lerp can make it slide to their health and that it scales with their health but I cannot seem to find a working way of doing it so I have come here.