Author Topic: Health Bar Issues  (Read 3119 times)

0 Members and 1 Guest are viewing this topic.

Offline Suicidal Dog

  • Newbie
  • *
  • Posts: 14
  • Karma: -1
Health Bar Issues
« on: May 07, 2017, 08:22:08 AM »
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: [Select]

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:


Code: [Select]

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:
Code: [Select]
[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.




Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Health Bar Issues
« Reply #1 on: May 07, 2017, 10:33:25 AM »
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)

Offline Suicidal Dog

  • Newbie
  • *
  • Posts: 14
  • Karma: -1
Re: Health Bar Issues
« Reply #2 on: May 07, 2017, 10:39:22 AM »
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.

Offline Suicidal Dog

  • Newbie
  • *
  • Posts: 14
  • Karma: -1
Re: Health Bar Issues
« Reply #3 on: May 08, 2017, 10:59:42 AM »
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.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Health Bar Issues
« Reply #4 on: May 08, 2017, 12:43:03 PM »
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.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Health Bar Issues
« Reply #5 on: May 08, 2017, 05:45:42 PM »
There are several ways, each with pros/cons.
Perhaps you could use PLAYER:Loadout to call that function then return as normal to prevent it from modifying loadout weapons.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Suicidal Dog

  • Newbie
  • *
  • Posts: 14
  • Karma: -1
Re: Health Bar Issues
« Reply #6 on: May 09, 2017, 02:56:26 PM »
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.

Dude I didn't mean to seem bossy and 'demand' anything but I literally have the most important time of my life in these few weeks with GCSEs and after doing stuff for school I'm tired and can't focus properly but now I've thought about it I've probably figured out an issue so don't jump to a conclusion I am too lazy to do things myself.

Offline Suicidal Dog

  • Newbie
  • *
  • Posts: 14
  • Karma: -1
Re: Health Bar Issues
« Reply #7 on: May 10, 2017, 10:36:33 AM »
When trying to do this is still doesn't work and sets the hud to an old look upon joining until I resave the file so it updates on the player.

Code: [Select]
if IsValid( LocalPlayer() ) then
local smoothHealth = LocalPlayer():GetMaxHealth()
end



local function healtharmorBase()

draw.RoundedBox( 0, 0, ScrH () - 75 - 10, 450, 75, Color( 0, 0, 0, 200 ) )

draw.RoundedBox( 0, 0, ScrH() - 85 - 60, 350, 50, Color( 0, 0, 0, 200 ))

surface.SetDrawColor( Color( 255, 255, 255, 255 ) )

surface.DrawOutlinedRect( -1, ScrH() - 83, 449, 71 )

surface.DrawOutlinedRect( -1, ScrH() - 143, 349, 46 )

end

local function healtharmorBar()

smoothHealth = Lerp( 5 * FrameTime(), smoothHealth, LocalPlayer():Health() )

draw.RoundedBox( 0, 0, ScrH() - 75- 5, 446 * smoothHealth /100, 65, Color( 200, 50, 50 ))

end

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Health Bar Issues
« Reply #8 on: May 10, 2017, 01:11:35 PM »
Code outside of functions gets called when the file is loaded.

Your valid checks need to be in the functions where the information is needed.

Offline Suicidal Dog

  • Newbie
  • *
  • Posts: 14
  • Karma: -1
Re: Health Bar Issues
« Reply #9 on: May 13, 2017, 05:34:25 AM »
This one isn't working but the 2nd one works on a different server I've got where I was doing gamemode stuff.


Code: [Select]
local function healtharmorBar()

maxHealth = LocalPlayer():GetMaxHealth()

smoothHealth = Lerp( 5 * FrameTime(), 100, LocalPlayer():Health() )

surface.SetDrawColor( 200, 50, 50, 255 )
--surface.DrawTexturedRect( 0, ScrH() - 75- 5, 445 * ( LocalPlayer():Health() / LocalPlayer():GetMaxHealth() ) , 65)
draw.RoundedBox( 0, 0, ScrH() - 85 - 55, 345 * smoothHealth / 100, 40,Color( 200, 50, 50 ))

surface.SetDrawColor( 50, 50, 255, 255 )
--surface.DrawTexturedRect( 0, ScrH() - 85 - 55, 345 * ( LocalPlayer():Health() / 100 ) , 40)
draw.RoundedBox( 0, 0, ScrH() - 70 - 11, 245 * ( LocalPlayer():Armor() / 100 ), 16, Color( 30, 30, 255 ) )


end

A short video on what happens when I try the first one on the screen: https://www.youtube.com/watch?v=02dwOjvSiIw&feature=youtu.be

From that video you see the health bar was jumping back and forth between random points it seems, now I've no idea what that could be caused by.

Code: [Select]
local smoothHealth = 250

local function HealthBar()

smoothHealth = Lerp( 5 * FrameTime(), smoothHealth, LocalPlayer():Health() )

draw.RoundedBox( 0, 12, ScrH() / 2 - 132, 96, 264, Color( 0, 0, 0, 175 ) )
draw.RoundedBox( 0, 19, ScrH() / 2 - 124.25, 82, smoothHealth, Color( 200, 50, 50, 255 ) )
draw.RoundedBox( 0, 12, ScrH() / 2 - 17.5, 96, 35, Color( 0, 0, 0, 175 ) )
surface.SetDrawColor( Color( 200, 200, 200, 255 ) )
surface.DrawOutlinedRect( 15, ScrH() / 2 - 129, 90, 258 )
surface.DrawOutlinedRect( 15, ScrH() / 2 - 16, 90, 30 )

draw.SimpleTextOutlined( LocalPlayer():Health(), "CloseCaption_Bold", 60, ScrH() / 2 - 1, Color( 255, 255, 255 ), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER , 1, Color( 0, 0, 0, 100 ) )

end