Ammo Count Bar Issue

When I try to make a ammo count bar (Like a health bar) I’m having trouble with it.

Code:



local clip = LocalPlayer():GetActiveWeapon():Clip1() or ""
draw.RoundedBox(0, ScrW() - 230, ScrH() - 30, (733) * clip / 100, 20, mHUD.AmmoBarColor)


I know how this code works, Im asking just how I can edit it to make the bar fit for all weapons

Now it is full when the clip has 30 rounds in it (Like the weapon has a max of 30 rounds per clip)
But if I use a weapon that can only hold 1 shot per clip (Half Life Crossbow)
And last if the clip can hold more that 30 rounds in the clip is goes over the ammo display (Half Life SMG)

GetMaxClip1 will get you the max amount of ammo the clip can hold so you can scale your bar with that. Also, change your clip variable to this instead to prevent errors:


local weapon = LocalPlayer():GetActiveWeapon()

if (weapon:IsValid()) then
   local clip = weapon:Clip1()
   -- Draw code
end

Does this look rigth?
This is my entire code for the ammo display



local function DrawAmmo()
	local weapon = LocalPlayer():GetActiveWeapon()
	if (weapon:IsValid()) then
        if (not table.HasValue(mHUD.WeaponsNotDraw, weapon:GetClass())) then
        local clip = weapon:Clip1() or ""
		local extra = LocalPlayer():GetAmmoCount(LocalPlayer():GetActiveWeapon():GetPrimaryAmmoType()) or ""

		// Base
		draw.RoundedBox(0, ScrW() - 230, ScrH() - 130, 220, 120, Color(35,35,35,255))
		draw.SimpleText(clip, "hudFont50", ScrW() - 175, ScrH() - 105, Color(255,255,255,255), center)
		draw.RoundedBox(0, ScrW() - 120, ScrH() - 120, 4, 80, Color(255,255,255,255))
		draw.SimpleText(extra, "hudFont50", ScrW() - 63, ScrH() - 105, Color(255,255,255,255), center)
		draw.SimpleText("Clip", "hudFont22Alt", ScrW() - 175, ScrH() - 65, Color(255,255,255,255), center)
		draw.SimpleText("Extra", "hudFont22Alt", ScrW() - 63, ScrH() - 65, Color(255,255,255,255), center)
		if (mHUD.AmmoCountBar == true) then
			draw.RoundedBox(0, ScrW() - 230, ScrH() - 30, 220, 20, Color(82,82,82,255))
			draw.RoundedBox(0, ScrW() - 230, ScrH() - 30, (220) * clip / 100, 20, mHUD.AmmoBarColor)
		else
			draw.RoundedBox(0, ScrW() - 120, ScrH() - 120, 4, 100, Color(255,255,255,255))
		end
		end
	end
end


You’re still doing “weapon:Clip1() or “””, don’t do that. Also, use weapon instead of LocalPlayer():GetActiveWeapon() in your “extra” declaration.

When I do weapon:GetMaxClip1() it makes it so the bar does not move at all

You should be dividing Clip1() by GetMaxClip1() and multiplying that number by the max length of the bar.



local bar_length = 200
local clip = wep:Clip1() / wep:GetMaxClip1()
bar_length = bar_length * clip


Then use the bar length var to set the width of the rounded box.

Works great thank you so much