• Print

Author Topic: Clip1() a nil value while dead.  (Read 6201 times)

0 Members and 1 Guest are viewing this topic.

Offline Dog

  • Newbie
  • *
  • Posts: 17
  • Karma: 1
Clip1() a nil value while dead.
« on: November 11, 2014, 07:22:41 pm »
Sup, another HUD problem. I was wondering if I can fix this lua error without removing it from the HUD. So basically this is what happens:
The ammo works when you have a weapon but if you don't console will spam a lua error saying Clip1() (Amount of bullets in clip) is a nil value. I wanna know if I can fix this without moving it to the weapon selection.
Here's the code:
Code: Lua
  1. function GM:HUDPaint()
  2.                 self.BaseClass:HUDPaint()
  3.                 local ply = LocalPlayer()
  4.                 local HP = LocalPlayer():Health()
  5.                 local wep = ply:GetActiveWeapon()
  6.                 local Ammo1 = LocalPlayer():GetActiveWeapon():Clip1()
  7.                 local Ammo2 = ply:GetAmmoCount( wep:GetPrimaryAmmoType() )
  8.                
  9.                
  10.                 surface.SetDrawColor(255,255,255,255)
  11.                 surface.SetMaterial( Material( "materials/pdhud/hp.png", "noclamp" ) )
  12.                 surface.DrawTexturedRect( 0, ScrH() / ScrH(), 42, 42 ) -- Draws HP Icon
  13.                 surface.SetTextColor( 255, 0, 0, 255 )
  14.                 surface.SetTextPos( 50, ScrH() / ScrH()+10 )
  15.                 surface.SetFont( "pd_basefont" )
  16.                 surface.DrawText( HP ) -- Puts HP Next to HP Icon
  17.                
  18.                 surface.SetDrawColor(255,255,255,255)
  19.                 surface.SetMaterial( Material( "materials/pdhud/ammo.png", "noclamp" ) )
  20.                 surface.DrawTexturedRect( 0, ScrH() / ScrH()+42, 42, 42 ) -- Draws Ammo Icon
  21.                 surface.SetTextColor( 255, 0, 0, 255 )
  22.                 surface.SetTextPos( 50, ScrH() / ScrH()+52 )
  23.                 surface.SetFont( "pd_basefont" )
  24.                 if ply:GetActiveWeapon():Clip1() <0 and ply:GetAmmoCount( wep:GetPrimaryAmmoType() ) == 0 then
  25.                         surface.DrawText( "Melee" )
  26.                 elseif ply:GetActiveWeapon():Clip1() <0 and ply:GetAmmoCount( wep:GetPrimaryAmmoType() ) >0 then
  27.                         surface.DrawText( Ammo2 .. " Rockets")
  28.                 else
  29.                         surface.DrawText( Ammo1 .. "/" .. Ammo2 ) -- <MagAmmo>/<OtherAmmo>
  30.                 end    
  31.         end
It's in the cl_init.lua of the gamemode.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Clip1() a nil value while dead.
« Reply #1 on: November 11, 2014, 08:13:33 pm »
Use an if/then and !ply:Alive(), or Ammo1, or wherever, = nil near whatever line of code the error chokes on, set a fake ammo1 variable count of 1 or something.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Dog

  • Newbie
  • *
  • Posts: 17
  • Karma: 1
Re: Clip1() a nil value while dead.
« Reply #2 on: November 13, 2014, 06:37:04 pm »
I get an error while dead:
[ERROR] gamemodes/jailbreak/gamemode/cl_init.lua:48: attempt to call method 'Cli
p1' (a nil value)
  1. unknown - gamemodes/jailbreak/gamemode/cl_init.lua:48
Line 48: local Ammo1 = LocalPlayer():GetActiveWeapon():Clip1()
Code: Lua
  1. function GM:HUDPaint()
  2.                 self.BaseClass:HUDPaint()
  3.                 local ply = LocalPlayer()
  4.                 if ply:Alive() then
  5.                         local HP = LocalPlayer():Health()
  6.                         local wep = ply:GetActiveWeapon()
  7.                         local Ammo1 = LocalPlayer():GetActiveWeapon():Clip1()
  8.                         local Ammo2 = ply:GetAmmoCount( wep:GetPrimaryAmmoType() )
  9.                        
  10.                        
  11.                         surface.SetDrawColor(255,255,255,255)
  12.                         surface.SetMaterial( Material( "materials/pdhud/hp.png", "noclamp" ) )
  13.                         surface.DrawTexturedRect( 0, ScrH() / ScrH(), 42, 42 ) -- Draws HP Icon
  14.                         surface.SetTextColor( 255, 0, 0, 255 )
  15.                         surface.SetTextPos( 50, ScrH() / ScrH()+10 )
  16.                         surface.SetFont( "pd_basefont" )
  17.                         surface.DrawText( HP ) -- Puts HP Next to HP Icon
  18.                        
  19.                         surface.SetDrawColor(255,255,255,255)
  20.                         surface.SetMaterial( Material( "materials/pdhud/ammo.png", "noclamp" ) )
  21.                         surface.DrawTexturedRect( 0, ScrH() / ScrH()+42, 42, 42 ) -- Draws Ammo Icon
  22.                         surface.SetTextColor( 255, 0, 0, 255 )
  23.                         surface.SetTextPos( 50, ScrH() / ScrH()+52 )
  24.                         surface.SetFont( "pd_basefont" )
  25.                         if ply:GetActiveWeapon():Clip1() <0 and ply:GetAmmoCount( wep:GetPrimaryAmmoType() ) == 0 then
  26.                                 surface.DrawText( "Melee" )
  27.                         elseif ply:GetActiveWeapon():Clip1() <0 and ply:GetAmmoCount( wep:GetPrimaryAmmoType() ) >0 then
  28.                                 surface.DrawText( Ammo2 .. " Rockets")
  29.                         else
  30.                                 surface.DrawText( Ammo1 .. "/" .. Ammo2 ) -- <MagAmmo>/<OtherAmmo>
  31.                         end
  32.                 else
  33.                         local HP = LocalPlayer():Health()
  34.                        
  35.                         surface.SetDrawColor(255,255,255,255)
  36.                         surface.SetMaterial( Material( "materials/pdhud/hp.png", "noclamp" ) )
  37.                         surface.DrawTexturedRect( 0, ScrH() / ScrH(), 42, 42 ) -- Draws HP Icon
  38.                         surface.SetTextColor( 255, 0, 0, 255 )
  39.                         surface.SetTextPos( 50, ScrH() / ScrH()+10 )
  40.                         surface.SetFont( "pd_basefont" )
  41.                         surface.DrawText( HP ) -- Puts HP Next to HP Icon
  42.                        
  43.                         surface.SetDrawColor(255,255,255,255)
  44.                         surface.SetMaterial( Material( "materials/pdhud/ammo.png", "noclamp" ) )
  45.                         surface.DrawTexturedRect( 0, ScrH() / ScrH()+42, 42, 42 ) -- Draws Ammo Icon
  46.                         surface.SetTextColor( 255, 0, 0, 255 )
  47.                         surface.SetTextPos( 50, ScrH() / ScrH()+52 )
  48.                         surface.SetFont( "pd_basefont" )
  49.                         surface.DrawText( "" )
  50.                 end
  51.         end

Offline Lukas692

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
Re: Clip1() a nil value while dead.
« Reply #3 on: November 27, 2016, 04:05:47 pm »
you fixed it (have same problem)?

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Clip1() a nil value while dead.
« Reply #4 on: November 30, 2016, 07:25:30 pm »
Iviscosity, please pay closer attention to detail.
The code you reference and respond to was posted over 2 years ago.
Hence why many of us didn't respond to the last question, not only was it a 'you should try it and see', it was a bad necro.
Dog hasn''t been here in a long time.
We don't mind necro posts if they truly ask thoughtful questions.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Clip1() a nil value while dead.
« Reply #5 on: November 30, 2016, 07:27:28 pm »
Iviscosity, please pay closer attention to detail.
The code you reference and respond to was posted over 2 years ago.
Hence why many of us didn't respond to the last question, not only was it a 'you should try it and see', it was a bad necro.
Dog hasn''t been here in a long time.
We don't mind necro posts if they truly ask thoughtful questions.

Oh crap, I didn't even notice... I'm really tired I'm sorry
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

  • Print