Do you know the Lua code for a HUD to only show when a player only has a gun out.

Does any one know how to make a HUD only show up when a client has a weapon out.

Thanks.

Use an IsValid check on this

Player:GetActiveWeapon

Or if its a custom weapon:

WEAPON/DrawHUD

but how would I fit it into this code




local function Base()
    local client = LocalPlayer()

    --HUD Backgrounds
    --Health and RP stuff Backgroud
    draw.RoundedBox(20,0 + 10 ,ScrH() - 100-10,400,100,Color(110,110,110,250))


    --Health
    draw.RoundedBox(6,0 +20,ScrH()-100,400 -20, 30,Color(200,200,200,255))
    local DrawHealth = LocalPlayer():Health() or 0
    local EcoHealth = LocalPlayer():Health() or 0

    if DrawHealth > 100 then DrawHealth = 100 end
    if DrawHealth < 0 then DrawHealth = 0 end

    if DrawHealth != 0 then
    draw.RoundedBox(6,0 +22,ScrH() -100 +2,(400-24) * DrawHealth / 100,30-4,Color(255,0,0,255))
end
    draw.DrawText( "Health: "..DrawHealth, "Trebuchet24", 0 + 24, ScrH () - 100 + 2, Color(255,255,255,255))

    --Armor
    draw.RoundedBox(6,0 +20,ScrH()-100+40,400 -20, 30,Color(200,200,200,255))
    local DrawArmor = client:Armor() or 0

    if DrawArmor > 100 then DrawArmor = 100 end 
    if DrawArmor < 0 then DrawArmor = 0 end

    if DrawArmor != 0 then 
    draw.RoundedBox(6, 0 + 22, ScrH()-100 +42,(400-24) * DrawArmor / 100, 30-4, Color(0,0,255,255))
    end
    draw.DrawText( "Armor: "..DrawArmor, "Trebuchet24", 0 + 24, ScrH () - 100 + 42, Color(255,255,255,255))

    --/*Name, Job, Salary, Wallet
    --draw.RoundedBox(6,0 + 22,ScrH()-200+80,400 -20,30,Color(200,200,200,255))
    --draw.DrawText("Name: "..client:Nick(),"Trebuchet24",0 + 24, ScrH () - 200 + 82,Color(255,255,255,255))
    --draw.DrawText("Job: "..client:getDarkRPVar("job"),"Trebuchet24",0 + 175, ScrH () - 200 + 82,Color(255,255,255,255))
    --draw.RoundedBox(6,0 + 22,ScrH()-200+120,400 -20,30,Color(200,200,200,255))
    --draw.DrawText("Salary: $"..client:getDarkRPVar("salary"),"Trebuchet24",0 + 24, ScrH () - 200 + 122,Color(255,255,255,255))
    --draw.DrawText("Money: $"..client:getDarkRPVar("money"),"Trebuchet24",0 + 175, ScrH () - 200 + 122,Color(255,255,255,255))----*/
    
    --Ammo HUD Background
    draw.RoundedBox(6,ScrW() -200-10 ,ScrH() - 100 -10 ,200,100,Color(110,110,110,250))
    --Ammo 1
    draw.RoundedBox(6,ScrW() -180-20,ScrH()-200+97,200-20,40,Color(255, 208, 0,255))
    local DrawAmmo1 = client:GetActiveWeapon():Clip1()
    draw.DrawText("Ammo: "..DrawAmmo1,"Trebuchet24",ScrW() -180+16 ,ScrH()-200+104,Color(255,255,255,255))
    --Ammo 2
    draw.RoundedBox(6,ScrW() -180-20,ScrH()-200+145,200-20,40,Color(255, 208, 0,255))
    local DrawAmmo2 = client:GetAmmoCount(client:GetActiveWeapon():GetPrimaryAmmoType())
    draw.DrawText("Ammo Extra: "..DrawAmmo2,"Trebuchet24",ScrW() -180+4 ,ScrH()-200+152,Color(255,255,255,255))
end


Old af rip me lmao -snip -



hook.Add( "HUDPaint", "MyHUD", function()
if not IsValid( LocalPlayer():GetActiveWeapon() ) or LocalPlayer():GetActiveWeapon():GetClass() ~= "weapon_crossbow" then return end

-- your code here
end )

so to fit with the OP’s code where “-- your code here” is put “Base()”

Sorry for bringing a old post back.
But aren’t there a way to define weapons that actually has bullets and not weapons like physgun, physcannon and crowbar?

Check if the ammo is >0. Physgun, toolgun, etc. register their Clip1 to be -1 so you can check based on that.

Or do what I do and create a list of weapons that don’t have ammo and run a “if !table.HasValue(x, y) then draw”



NoAmmoWeapons = {
	"weapon_bugbait", 
	"weapon_crowbar", 
	"weapon_physcannon", 
	"weapon_stunstick", 
	"weapon_fists", 
	"manhack_welder", 
	"weapon_physgun", 
	"gmod_tool"
}


Hey again.

Thank you for your reply.
It worked as you said the first one.

If anyone wants the code. Take it :slight_smile:



if (LocalPlayer():GetActiveWeapon():GetPrimaryAmmoType() >= 0) then
    if (LocalPlayer():GetActiveWeapon():Clip1() != -1) then

        // Your code

    end
end