SWEP:DrawHud() acting funny

My swep will display the initial value of Timer on my screen but will not change when the value Timer changes, I’ll try to post only the relevent code



local Timer = 1

function SWEP:DrawHUD()
	if(!self.Owner:Alive()) then return; end
	
	draw.DrawText(
		"Timer: " .. Timer,
		"ChatFont",
		ScrW() - 20,
		ScrH() - draw.GetFontHeight("ChatFont") - 20,
		Color(255, 255, 255, 255),
		TEXT_ALIGN_RIGHT
	)
end

// this is the only place the Timer value gets changed, it just toggles the Timer value through 1,2,3 and 5
function SWEP:SecondaryAttack()
	self:EmitSound( ClickSound )
	
	if Timer == 1 then
		Timer = 2
	elseif Timer == 2 then
		Timer = 3
	elseif Timer == 3 then
		Timer = 5
	elseif Timer == 5 then
		Timer = 1
	else
		Timer = 1
	end
end


If anyone could help me I’d give you infinite hugs.

local timer = 1
That could be a problem. Every time the script runs, it sets the timer to 1.
Also, the last else statement could cause problems as

What is your main goal with this? What is the timer supposed to be for, reload?

This is the swep’s shared.lua, I’m pretty sure it’s only being executed once and I am sure Timer is at the correct value it’s just not being displayed by SWEP:DrawHUD() correctly.

The timer sets the timer of a grenade launched from the swep (The timer value is being set correctly, it’s just not being displayed right).

Oh no, why are you not just using
SWEP:SetNextSecondaryFire( CurTime() + 1 )
or timer library instead?
You would need hundreds of if statements in order to make even a small delay.

I’m not sure what you think I’m doing but don’t get your knickers in a knot, it’s just a npc_grenade_frag (the entity that comes out of the player when you throw a grenade) with ent:Fire(“SetTimer”, Timer, 0) to set the timer on the grenade when it’s first spawned, and right click to toggle the Timer value between 1,2,3 and 5.

But my HUD still isn’t updating properly or something, does anyone know how to fix this?

Can you post all of the code?

Does the timer actually work, or does it just not display the value?

Yea, everything works except for displaying the value on the screen

Ok, I just didn’t want to make people read through a wall of text


SWEP.Author			= ""
SWEP.Contact		= ""
SWEP.Purpose		= ""
SWEP.Instructions	= "Shoots grenades"

SWEP.Spawnable			= true
SWEP.AdminSpawnable		= true

SWEP.ViewModel			= "models/weapons/v_pistol.mdl"
SWEP.WorldModel			= "models/weapons/w_pistol.mdl"

SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= -1
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "none"

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"

local ShootSound = Sound( "weapons/grenade_launcher1.wav" )
local ClickSound = Sound( "common/wpn_select.wav" )
local Timer = 1

/*---------------------------------------------------------
	DrawHUD
---------------------------------------------------------*/
function SWEP:DrawHUD()
	if(!self.Owner:Alive()) then return; end
	
	draw.DrawText(
		"Timer: " .. Timer,
		"ChatFont",
		ScrW() - 20,
		ScrH() - draw.GetFontHeight("ChatFont") - 20,
		Color(255, 255, 255, 255),
		TEXT_ALIGN_RIGHT
	)
end

/*---------------------------------------------------------
	PrimaryAttack
---------------------------------------------------------*/
function SWEP:PrimaryAttack()
	self:EmitSound( ShootSound )

	self:ShootEffects( self )
	
	// The rest is only done on the server
	if (!SERVER) then return end
	
	local ang = self.Owner:GetAimVector()
	
	local ent = ents.Create( "npc_grenade_frag" )
	ent:SetPos( self.Owner:EyePos() + (ang * 50) )
	ent:SetAngles( self.Owner:EyeAngles() + Angle(90, 0, 0))
	ent:Spawn()
	ent:Fire("SetTimer", Timer, 0)
	
	ent:GetPhysicsObject():ApplyForceCenter(ang * 3500)
	
	cleanup.Add(self.Owner, "props", ent)
end

/*---------------------------------------------------------
	SecondaryAttack
---------------------------------------------------------*/
function SWEP:SecondaryAttack()
	self:EmitSound( ClickSound )
	
	if Timer == 1 then
		Timer = 2
	elseif Timer == 2 then
		Timer = 3
	elseif Timer == 3 then
		Timer = 5
	elseif Timer == 5 then
		Timer = 1
	else
		Timer = 1
	end
end