General > Developers Corner
Health Bar Issues
Suicidal Dog:
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.
--- Code: ---
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
--- End code ---
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:
--- Code: ---
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
--- End code ---
the second one works but only if the player is loaded in and isn't invalid as it gives me the error:
--- Code: ---[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
--- End code ---
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.
MrPresident:
Just have the function return nothing and not even try to draw a bar until they are loaded and valid.
you can do this a couple ways. You can check the existence of the value you are checking for and return out of the function if it's non-existent, or you can create some kind of ready check that is set on the player when they fully load in and have clientside code check to see if they're ready. (That's what I do in my gamemodes)
Suicidal Dog:
I've had this issue before and never actually find a fix if you could show me how and label it so I can understand it myself.
Suicidal Dog:
Please can someone respond I need a fix and ASAP and I am currently rushed for time lately so I can't do much research into these things.
MrPresident:
I gave you the way to do it. Please do not come on here demanding that people do work for you. This forum is for getting assistance with things, not for having people do things for you.
If you can't be bothered to do the research yourself.. then no one else can be bothered to do it for you.
Navigation
[0] Message Index
[#] Next page
Go to full version