Getting the current users weapon and put a image on the hud

This is what i got so far and its not working im a little stuck anyone have a idea on how to do this?



                surface.SetDrawColor( 255, 255, 255, 255 )
		if ply:GetActiveWeapon("weapon_smg1") then
			smg = Material( "materials/serioushud/weapon_smg1_image.png" )
			surface.SetMaterial( smg )
		elseif ply:GetActiveWeapon("weapon_shotgun") then
			shotgun = Material( "materials/serioushud/weapon_shotgun_image.png" )
			surface.SetMaterial( shotgun )
		end
		surface.DrawTexturedRect( ScrW()/1.92, ScrH()/1.16, ScrW()/20.39, ScrH()/12 )


Firstly, it isn’t working because your file path has ‘materials’ at the start of it. If you remove that bit it should work.

Secondly, you could do this without having to make a different image for every weapon by using a DModelPanel with the weapon’s model. Don’t create it every frame, though.

Thirdly, please cache that material! It even says to do that on the example of

surface.DrawTexturedRect. Using the Material function every frame is costly.

Fourtly, please create those variables locally rather than making them global, if they’re global, another script could accidentally use them and screw up your script.

This version of your code will probably work if you don’t want to use a DModelPanel:



local weapon = 'Whatever you want the default weapon image to be, or just nil'
if ply:GetActiveWeapon("weapon_smg1") then
	weapon = Material( "serioushud/weapon_smg1_image.png" )
elseif ply:GetActiveWeapon("weapon_shotgun") then
	weapon = Material( "serioushud/weapon_shotgun_image.png" )
end

surface.SetDrawColor( 255, 255, 255, 255 )
surface.SetMaterial( weapon )
surface.DrawTexturedRect( ScrW()/1.92, ScrH()/1.16, ScrW()/20.39, ScrH()/12 )


i got it show the smg but it only shows the smg and i want it to change if they scroll and change there weapon

Wait, so are you using a DModelPanel or that code I posted? What are you using?

the one you posted was just like mine and i dont know much about DModelPanel so i started to mess with it

I think its the


 ply:GetActiveWeapon 

thats messing up

Actually, I think it is. You can’t put a weapon string in the brackets, it won’t do anything. You need to do



ply:GetActiveWeapon():GetClass() == "weapon_smg1"


your a genius