1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Sign up for news, events and more!

    You're currently visiting the official DarkRP Forums as a guest. Sign up now to participate in our community and we'll let you know when we have news.

HUD Help

Discussion in 'DarkRP Modding Questions & Help' started by Vex, Feb 18, 2015.

  1. Vex

    Vex New Member

    Heya I am making a HUD however I am having an issue here and it spits out an error whenever the LocalPlayer does not have a weapon currently this is the code,
    Code (Text):

    -- Drawing the Clip and Maxclip
    local function AmmoDraw()

    draw.RoundedBox( 0, ScrW()-290, ScrH()-40, 300-21, 30, Color(90,90,90,235 ) )

    local DrawClip = LocalPlayer():GetActiveWeapon():Clip1() or 0
    local DrawMax = LocalPlayer():GetActiveWeapon():GetPrimaryAmmoType() or 0

    if LocalPlayer():GetActiveWeapon() = nil then return false end
    if DrawClip < 0 then DrawClip = 0 end
    if DrawMax < 0 then DrawMax = 0 end

    draw.DrawText( 'Ammo - ' .. DrawClip , 'dev_font', ScrW()-290+10, ScrH()-40+3.5, Color(255,255,255,255) )
    draw.DrawText( '/ '.. DrawMax, 'dev_font', ScrW()-290+110, ScrH()-40+3.5, Color(255,255,255,255) )

    end
     
    That is not the only issue, GetPrimaryAmmoType() is not the var I am looking for does anyone know which one it is that displays a SWEPS Reserve ammo?

    EG I have a pistol with 30 in a clip and 60 in reserve ..... 30/60 I shot a clip so now I have 30/30 etc

    Cheers for even looking!
     
    Last edited: Feb 18, 2015
  2. Matryan

    Matryan Well-Known Member

    1. What error does it spit out?
    2. The function you're looking for would be
    Code (Text):
    LocalPlayer():GetAmmoCount(LocalPlayer():GetActiveWeapon():GetPrimaryAmmoType()) -- ???
     
  3. Vex

    Vex New Member

    The error is

    [ERROR] addons/darkrpmodification/lua/darkrp_modules/hudreplacement/cl_hudreplacement.lua:94: attempt to call method 'Clip1' (a nil value)
    1. AmmoDraw - addons/darkrpmodification/lua/darkrp_modules/hudreplacement/cl_hudreplacement.lua:94
    2. fn - addons/darkrpmodification/lua/darkrp_modules/hudreplacement/cl_hudreplacement.lua:423
    3. unknown - addons/ulib/lua/ulib/shared/hook.lua:183

    and that is doing the same thing it doesn't update the weapon for instance I spawn a P228 it says I have 12/3
    I finish a clip and it still says 0/3
     
  4. Matryan

    Matryan Well-Known Member

    3?
    It worked for me in a HUD I made for a community a bit ago.
    Code (Text):
    ammo = LocalPlayer():GetActiveWeapon():Clip1().." / "..LocalPlayer():GetAmmoCount(LocalPlayer():GetActiveWeapon():GetPrimaryAmmoType())
    Can you show me line 94 of your HUD Replacement?
     
  5. Vex

    Vex New Member

    [​IMG]
    Ammo on a brand new weapon only has 30 bullets, still shows it has 3 clips
    [​IMG]
    When the weapon is out of ammo, the clip still doesn't update

    Code (Text):
    local DrawClip = LocalPlayer():GetActiveWeapon():Clip1() or 0
     
  6. Matryan

    Matryan Well-Known Member

    Chuck us the line of code you used to draw the ammo / the amount of ammo left.

    Not sure about that Clip1() error though....
     
  7. Vex

    Vex New Member

    Alrighty I will throw in the general code for both of the ammo clips

    Code (Text):
    local DrawClip = LocalPlayer():GetActiveWeapon():Clip1() or 0
    local DrawMax = LocalPlayer():GetAmmoCount(LocalPlayer():GetActiveWeapon():GetPrimaryAmmoType()) or 0

    if DrawClip < 0 then DrawClip = 0 end
    if DrawMax < 0 then DrawMax = 0 end
    -----EDIT----
    I think the Error is because said player has no weapon and its referring to the variable as nil, I am not sure how to fix either way
     
  8. Matryan

    Matryan Well-Known Member

    Hmm not sure about why it's not working.
    Have you tried it on another SWep / ammotype?

    Being dead is usually the only cause for users not having weapons:
    Code (Text):
    if LocalPlayer():Alive() then

        local DrawClip = LocalPlayer():GetActiveWeapon():Clip1()

    else

    local DrawClip = 0

    end
     
  9. Vex

    Vex New Member

    Hmm confused me a tad, should I put that in the same function or create a new one
     
  10. Matryan

    Matryan Well-Known Member

    Replace your
    Code (Text):
    local DrawClip = LocalPlayer():GetActiveWeapon():Clip1() or 0
    with it.
     
  11. Vex

    Vex New Member

    Tried it, then it spits out this error and messes the whole HUD up

    [ERROR] addons/darkrpmodification/lua/darkrp_modules/hudreplacement/cl_hudreplacement.lua:90: attempt to call method 'Alive' (a nil value)
    1. unknown - addons/darkrpmodification/lua/darkrp_modules/hudreplacement/cl_hudreplacement.lua:90
    2. doInclude - [C]:-1
    3. loadModules - gamemodes/darkrp/gamemode/libraries/modificationloader.lua:86
    4. fn - gamemodes/darkrp/gamemode/libraries/modificationloader.lua:126
    5. unknown - addons/ulib/lua/ulib/shared/hook.lua:183
     
  12. Matryan

    Matryan Well-Known Member

    0_0...
    Alive() is definitely a valid function.
    I'm not sure what's going on...
     
  13. Vex

    Vex New Member

    It's certainly so odd I will play around and post results
     
  14. Vex

    Vex New Member

    [ERROR] addons/darkrpmodification/lua/darkrp_modules/hudreplacement/cl_hudreplacement.lua:108: attempt to concatenate global 'DrawClip' (a nil value)
    1. AmmoDraw - addons/darkrpmodification/lua/darkrp_modules/hudreplacement/cl_hudreplacement.lua:108
    2. fn - addons/darkrpmodification/lua/darkrp_modules/hudreplacement/cl_hudreplacement.lua:431
    3. unknown - addons/ulib/lua/ulib/shared/hook.lua:183

    New one fixed it up a bit, not sure what the issue could be now though here is the full function

    Code (Text):
    -- Drawing the Clip and Maxclip
    local function AmmoDraw()

    draw.RoundedBox( 0, ScrW()-290, ScrH()-40, 300-21, 30, Color(90,90,90,235 ) )

    if LocalPlayer():Alive() then

        local DrawClip = LocalPlayer():GetActiveWeapon():Clip1()

    else

    local DrawClip = 0

    end

    local DrawMax = LocalPlayer():GetAmmoCount(LocalPlayer():GetActiveWeapon():GetPrimaryAmmoType()) or 0

    if DrawMax < 0 then DrawMax = 0 end

    draw.DrawText( 'Ammo - ' .. DrawClip , 'dev_font', ScrW()-290+10, ScrH()-40+3.5, Color(255,255,255,255) )
    draw.DrawText( '/ '.. DrawMax, 'dev_font', ScrW()-290+110, ScrH()-40+3.5, Color(255,255,255,255) )

    end

    ------EDIT -------
    Moved the end to the bottom no errors but the text has completely disspeared now. so confused

    Code (Text):
    local function AmmoDraw()

    draw.RoundedBox( 0, ScrW()-290, ScrH()-40, 300-21, 30, Color(90,90,90,235 ) )

        if LocalPlayer():Alive() then

            local DrawClip = LocalPlayer():GetActiveWeapon():Clip1()

        else

        local DrawClip = 0



        local DrawMax = LocalPlayer():GetAmmoCount(LocalPlayer():GetActiveWeapon():GetPrimaryAmmoType()) or 0

            if DrawMax < 0 then DrawMax = 0 end

            draw.DrawText( 'Ammo - ' .. DrawClip , 'dev_font', ScrW()-290+10, ScrH()-40+3.5, Color(255,255,255,255) )
            draw.DrawText( '/ '.. DrawMax, 'dev_font', ScrW()-290+110, ScrH()-40+3.5, Color(255,255,255,255) )

        end
    end
     
  15. Matryan

    Matryan Well-Known Member

    Don't move the ends around randomly.
    They were fine as they were.
    I think it's because we're declaring that local variable inside an if statement (Not entirely sure).
     
  16. Vex

    Vex New Member

    If I dont move that end, it causes script errors and breaks everything, and I am not sure I am out of ideas here
     
  17. Matryan

    Matryan Well-Known Member

    It fixed an error but did it fix the HUD.
     
  18. Vex

    Vex New Member

    Fixed the HUD, however still spits out an error when player dies
    [ERROR] addons/darkrpmodification/lua/darkrp_modules/hudreplacement/cl_hudreplacement.lua:103: attempt to call method 'GetPrimaryAmmoType' (a nil value)
    1. AmmoDraw - addons/darkrpmodification/lua/darkrp_modules/hudreplacement/cl_hudreplacement.lua:103
    2. fn - addons/darkrpmodification/lua/darkrp_modules/hudreplacement/cl_hudreplacement.lua:437
    3. unknown - addons/ulib/lua/ulib/shared/hook.lua:183

    and also no text is showing where the ammo is suppose to be meaning the 20/60
     

Share This Page