• Need help with making a Custom HUD please.
    7 replies, posted
Hey all, Jacklin again! Im making a custom HUD and currently I have it Show The Health, The Current Armor/Suit, now what I need is for it to show the ammo, I read up on [url]http://wiki.garrysmod.com/page/Player/GetAmmoCount[/url] about get ammo count but the functions I see there don't seem to work, here is my cl_init.lua: [CODE]//Custom HUD Start local function HUD() local ply = LocalPlayer() if ( not IsValid( ply ) ) then return end local ply = LocalPlayer() local HP = ply:Health() local Armor = ply:Armor() local Ammo = 0 local YEL = Color( 255, 150, 0 ) local ADMINCOL = Color( 255, 236, 66 ) local WHT = Color( 255, 255, 255 ) local BLUG = Color( 0, 166, 99 ) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6) + 235 , ScrH() - 115, 240, 40, Color( 40, 40, 40, 255 ) ) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6) - 235 , ScrH() - 115, 240, 40, Color( 40, 40, 40, 255 ) ) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6) - 240, ScrH() - 80, 720, 45, Color( 30, 30, 30, 255 ) ) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6), ScrH() - 185, 240, 115, Color( 30, 30, 30, 255 ) ) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6) + 20 - 235, ScrH() - 105, 200, 20, Color( 255, 255, 255, 15 ) ) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6) + 20 - 235, ScrH() - 105, math.Clamp( Armor, 1, 100 )*2, 20, Color( 0, 172, 221, 255 ) ) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6) + 20 + 235, ScrH() - 105, 200, 20, Color( 255, 255, 255, 15 ) ) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6) + 20 + 235, ScrH() - 105, math.Clamp( Ammo, 1, 100 )*2, 20, YEL ) draw.DrawText("Armor", "DebugFixed", (ScrW() / 4) + (ScrW() / 6) + 50 - 235, ScrH() - 105, Color( 255, 255, 255, 255 ),TEXT_ALIGN_CENTER) draw.DrawText( Armor, "DebugFixed", (ScrW() / 4) + (ScrW() / 6) + 80 -235, ScrH() - 105, Color( 255, 255, 255, 255 ),TEXT_ALIGN_CENTER) draw.DrawText("(Broken)", "DebugFixed", (ScrW() / 4) + (ScrW() / 6) + 520 - 235, ScrH() - 105, Color( 255, 255, 255, 255 ),TEXT_ALIGN_CENTER) draw.DrawText( Ammo, "DebugFixed", (ScrW() / 4) + (ScrW() / 6) + 550 -235, ScrH() - 105, Color( 255, 255, 255, 255 ),TEXT_ALIGN_CENTER) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6) - 230, ScrH() - 70, 700, 20, Color( 255, 255, 255, 15 ) ) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6) - 230, ScrH() - 70, math.Clamp( HP, 1, 100 )*7, 20, Color( 255, 41, 33, 255 ) ) draw.DrawText("Health", "DebugFixed", (ScrW() / 4) + (ScrW() / 6) - 200, ScrH() - 70, Color( 255, 255, 255, 255 ),TEXT_ALIGN_CENTER) draw.DrawText( HP, "DebugFixed", (ScrW() / 4) + (ScrW() / 6) - 150, ScrH() - 70, Color( 255, 255, 255, 255 ),TEXT_ALIGN_CENTER) draw.TexturedQuad { texture = surface.GetTextureID "vgui/logo", color = Color(255, 255, 255, 255), x = (ScrW() / 4) + (ScrW() / 6) + 20, y = ScrH() - 215, w = 200, h = 180 } end hook.Add( "HUDPaint", "HUD", HUD ) //Custom HUD End[/CODE] (I know its probably un-neat and also probably bad coding, but im a beginner :eng101: and we all start/started somewhere.) Hope Someone can help! -Jacklin
Check out [url]http://wiki.garrysmod.com/page/Weapon/Clip1[/url] and [url]http://wiki.garrysmod.com/page/Weapon/Clip2[/url] I know cosmic seagull does this in his huds i believe laso there ssome good ones with this just use google
Wow such a quick response! thanks! I'll give these 2 links a read and see if i can add it to my code, as for seagull, well i dont think i have ever seen him do ammo (most of my code is what i have learnt from him) [editline]12th August 2014[/editline] ok so either this doesn't work any more OR im doing to completely wrong XD here is what i have: [CODE] local PrimAmmo = Weapon:Clip1( ) local SeconAmmo = Weapon:Clip2( )[/CODE]
[url]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/indexda13.html[/url] check out this tutorial
Okay so My cl_init.lua is as shown below, it works but if the player dies it caused lua errors and the ragdoll disapears, probably because there is no IF asking if the player is alive, but im not too sure, also how would i go about removing spawn menu tabs? once i get these 2 problems solved I can leave the server as it is because then it will be running fine :D [CODE]//Custom HUD Start local function HUD() local ply = LocalPlayer() if ( not IsValid( ply ) ) then return end local ply = LocalPlayer() local HP = ply:Health() local Armor = ply:Armor() local PrimAmmo = ply:GetActiveWeapon():Clip1() local SeconAmmo = ply:GetAmmoCount(ply:GetActiveWeapon():GetSecondaryAmmoType()) local ExtraAmmo = ply:GetAmmoCount(ply:GetActiveWeapon():GetPrimaryAmmoType()) local YEL = Color( 255, 150, 0 ) local ADMINCOL = Color( 255, 236, 66 ) local WHT = Color( 255, 255, 255 ) local BLUG = Color( 0, 166, 99 ) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6) + 235 , ScrH() - 115, 240, 40, Color( 40, 40, 40, 255 ) ) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6) + 235 , ScrH() - 150, 240, 40, Color( 40, 40, 40, 255 ) ) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6) - 235 , ScrH() - 115, 240, 40, Color( 40, 40, 40, 255 ) ) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6) - 240, ScrH() - 80, 720, 45, Color( 30, 30, 30, 255 ) ) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6), ScrH() - 185, 240, 115, Color( 30, 30, 30, 255 ) ) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6) + 20 - 235, ScrH() - 107, 200, 20, Color( 255, 255, 255, 15 ) ) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6) + 20 - 235, ScrH() - 107, math.Clamp( Armor, 1, 100 )*2, 20, Color( 0, 172, 221, 255 ) ) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6) + 20 + 235, ScrH() - 109, 90, 20, Color( 255, 255, 255, 15 ) ) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6) + 20 + 235, ScrH() - 109, math.Clamp( PrimAmmo, 1, 30 )*3, 20, YEL ) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6) + 20 + 355, ScrH() - 109, 90, 20, Color( 255, 255, 255, 15 ) ) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6) + 20 + 355, ScrH() - 109, math.Clamp( ExtraAmmo, 1, 30 )*3, 20, YEL ) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6) + 20 + 235, ScrH() - 139, 210, 20, Color( 255, 255, 255, 15 ) ) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6) + 20 + 235, ScrH() - 139, math.Clamp( SeconAmmo, 1, 30 )*7, 20, YEL ) draw.DrawText("Armor", "DebugFixed", (ScrW() / 4) + (ScrW() / 6) + 50 - 235, ScrH() - 105, Color( 255, 255, 255, 255 ),TEXT_ALIGN_CENTER) draw.DrawText( Armor, "DebugFixed", (ScrW() / 4) + (ScrW() / 6) + 80 - 235, ScrH() - 103, Color( 255, 255, 255, 255 ),TEXT_ALIGN_CENTER) draw.DrawText(" Primary", "DebugFixed", (ScrW() / 4) + (ScrW() / 6) + 520 - 250, ScrH() - 107, Color( 255, 255, 255, 255 ),TEXT_ALIGN_CENTER) draw.DrawText( PrimAmmo, "DebugFixed", (ScrW() / 4) + (ScrW() / 6) + 80 + 245, ScrH() - 107, Color( 255, 255, 255, 255 ),TEXT_ALIGN_CENTER) draw.DrawText(" Mag", "DebugFixed", (ScrW() / 4) + (ScrW() / 6) + 520 - 145, ScrH() - 107, Color( 255, 255, 255, 255 ),TEXT_ALIGN_CENTER) draw.DrawText( ExtraAmmo, "DebugFixed", (ScrW() / 4) + (ScrW() / 6) + 80 + 335, ScrH() - 107, Color( 255, 255, 255, 255 ),TEXT_ALIGN_CENTER) draw.DrawText( SeconAmmo, "DebugFixed", (ScrW() / 4) + (ScrW() / 6) + 80 + 260, ScrH() - 137, Color( 255, 255, 255, 255 ),TEXT_ALIGN_CENTER) draw.DrawText(" Secondary", "DebugFixed", (ScrW() / 4) + (ScrW() / 6) + 520 - 250, ScrH() - 137, Color( 255, 255, 255, 255 ),TEXT_ALIGN_CENTER) // draw.DrawText( Ammo, "DebugFixed", (ScrW() / 4) + (ScrW() / 6) + 550 -235, ScrH() - 105, Color( 255, 255, 255, 255 ),TEXT_ALIGN_CENTER) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6) - 230, ScrH() - 70, 700, 20, Color( 255, 255, 255, 15 ) ) draw.RoundedBox( 4, (ScrW() / 4) + (ScrW() / 6) - 230, ScrH() - 70, math.Clamp( HP, 1, 100 )*7, 20, Color( 255, 41, 33, 255 ) ) draw.DrawText("Health", "DebugFixed", (ScrW() / 4) + (ScrW() / 6) - 200, ScrH() - 70, Color( 255, 255, 255, 255 ),TEXT_ALIGN_CENTER) draw.DrawText( HP, "DebugFixed", (ScrW() / 4) + (ScrW() / 6) - 150, ScrH() - 70, Color( 255, 255, 255, 255 ),TEXT_ALIGN_CENTER) draw.TexturedQuad { texture = surface.GetTextureID "vgui/logo", color = Color(255, 255, 255, 255), x = (ScrW() / 4) + (ScrW() / 6) + 20, y = ScrH() - 215, w = 200, h = 180 } end hook.Add( "HUDPaint", "HUD", HUD ) //Custom HUD End[/CODE]
Try doing if not IsAlive thing in there and post the exact error please
[ERROR] gamemodes/sandbox/gamemode/cl_init.lua:199: attempt to call method 'Clip1' (a nil value) 1. fn - gamemodes/sandbox/gamemode/cl_init.lua:199 2. unknown - addons/ulib/lua/ulib/shared/hook.lua:183
I do this: [code] HUD = {} function HUD:Ammo() if !( LocalPlayer():GetActiveWeapon() and IsValid( LocalPlayer():GetActiveWeapon() ) ) then return end local pl = LocalPlayer() local w, h = ScrW(), ScrH() local ammo = pl:GetActiveWeapon():Clip1() local ammo2 = pl:GetAmmoCount( pl:GetActiveWeapon():GetPrimaryAmmoType() ) draw.RoundedBox( 4, ( h * 1.48 ) , ( w * 0.495 ) , ( h * 0.28 ), ( w * 0.051 ) , Color( 0, 0, 0, 220 ) ) draw.SimpleText( ammo, "Shit3", ( h * 1.5 ) , ( w * 0.5 ) , Color( 255, 255, 255, 255 ), TEXT_ALIGN_RIGHT, TEXT_ALIGN_BOTTOM ) draw.SimpleText( "/ "..ammo2, "Shit3", ( h * 1.6 ) , ( w * 0.513 ) , Color( 255, 255, 255, 255 ), TEXT_ALIGN_RIGHT, TEXT_ALIGN_BOTTOM ) end hook.Add("HUDPaint", "Hud", function() local pl = LocalPlayer() if ( IsValid( pl ) and pl:Team() == 1 ) then HUD:Ammo() end end ) [/code] You will have to change the font, otherwise it will throw a silly error at you. Basically, you din't check if ply had any weapon or ammo for that matter, so it tried to extract a value that isn't there. also, localize: [code] ScrW(), ScrH() [/code] and use ( w * 0.012 ) instead of ( w / 67 ) [B]Edit:[/B] Left some stuff in there you don't need.
Sorry, you need to Log In to post a reply to this thread.