• Print

Author Topic: Errors when in jail  (Read 4185 times)

0 Members and 1 Guest are viewing this topic.

Offline DankNessProvides

  • Newbie
  • *
  • Posts: 26
  • Karma: -3
Errors when in jail
« on: April 11, 2016, 05:31:25 am »
I am getting an error with getting the print name of the weapon when the player is either stripped or in jail on darkrp how would I go about getting it to actually check if the player is either stripped of weapons or is in jail.

Code: Lua
  1.         if LocalPlayer():Alive() and LocalPlayer():GetActiveWeapon():GetPrintName() != nil then
  2.                 draw.SimpleText( LocalPlayer():GetActiveWeapon():GetPrintName(), "Trebuchet24", ScrW() / 2 + 690, ScrH() / 2 + 305, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP)
  3.         end

This code stops the error but removes the weapon name:

Code: Lua
  1.         if LocalPlayer():Alive() and LocalPlayer():GetActiveWeapon != nil then
  2.                 if LocalPlayer():GetActiveWeapon():GetPrintName() != nil then
  3.                         draw.SimpleText( LocalPlayer():GetActiveWeapon():GetPrintName(), "Trebuchet24", ScrW() / 2 + 690, ScrH() / 2 + 305, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP)
  4.                 end
  5.         end

Offline Buzzkill

  • Respected Community Member
  • Full Member
  • *****
  • Posts: 176
  • Karma: 59
    • The Hundred Acre Bloodbath
Re: Errors when in jail
« Reply #1 on: April 11, 2016, 12:45:35 pm »
Using an unchecked function return value as the object for another function call is an anti-pattern.  You should be checking all of those to make sure they are valid. Additionally, not every weapon will have a Print Name defined, necessarily.  They should -- but not a guarantee.



Code: [Select]
if ( IsValid( LocalPlayer() ) ) then
if ( IsValid( LocalPlayer():GetActiveWeapon() ) ) then
draw.SimpleText( LocalPlayer():GetActiveWeapon():GetPrintName() or "NoPrintNameDefined", "Trebuchet24", ScrW() / 2 + 690, ScrH() / 2 + 305, Color(255,255,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP)
end
end


Quote
getting the print name of the weapon when the player is either stripped or in jail

Well, when they don't have any active weapon, what do you expect GetActiveWeapon() to return?  :)
« Last Edit: April 11, 2016, 12:47:35 pm by Buzzkill »

Offline DankNessProvides

  • Newbie
  • *
  • Posts: 26
  • Karma: -3
Re: Errors when in jail
« Reply #2 on: April 13, 2016, 12:15:29 pm »
What I want to know is what is the DarkRP variable or how do I check to see if the player is in arrested.

Offline Buzzkill

  • Respected Community Member
  • Full Member
  • *****
  • Posts: 176
  • Karma: 59
    • The Hundred Acre Bloodbath

  • Print