Secondary Attack problem.

Hi, I’m creating a swep. I changed secondary attack into a “switch” for fire modes. Take a look:



function SWEP:SecondaryAttack()
	
	self:EmitSound("npc/turret_floor/click1.wav")
	self:SetNextPrimaryFire(CurTime() + 0.5)
	
	
	if (self.mode == 0) then
		self.mode = 1
		self.Owner:PrintMessage(HUD_PRINTCENTER, "Ammo Type: Bullets")
		print(self.mode)
		
	end
	if (self.mode == 1) then 
		self.mode = 2
		self.Owner:PrintMessage(HUD_PRINTCENTER, "Ammo Type: Grenade")
		print(self.mode)
		
	end
	if (self.mode == 2) then
		self.mode = 0
		self.Owner:PrintMessage(HUD_PRINTCENTER, "Ammo Type: Air strike")
		print(self.mode)
		
	end
	self:SetNextSecondaryFire(CurTime() + 0.1)
	
	
	
end


When there was only 2 fire modes everything was fine but when I added a third fire mode gun starts crashing.
Here is log (1,2,0 are firemodes):



<right click>
1
2
0


but it should be:



<right click>
1
<right click>
2
<right click>
0


Can somebody help me?

[LUA]
if (self.mode == 2) then
self.mode = 0
self.Owner:PrintMessage(HUD_PRINTCENTER, “Ammo Type: Air strike”)
print(self.mode)

end

[/LUA]

You’re setting self.mode back to 0? Why’s this?

Because scrolling modes.

@OP: SWEP.Secondary.Automatic must be set to FALSE

Makes sense now that I think about it. I’ve never created sweps :stuck_out_tongue:

I already solved it :slight_smile: Just need to use else or else if:



function SWEP:SecondaryAttack()
	
	self:EmitSound("npc/turret_floor/click1.wav")
	
	
	if (self.mode == 0) then
		self.mode = 1
		self.Owner:PrintMessage(HUD_PRINTCENTER, "Ammo Type: Grenade")
		print(self.mode)
	else if (self.mode == 1) then
		self.mode = 2
		self.Owner:PrintMessage(HUD_PRINTCENTER, "Ammo Type: Headcrab Canister Air strike")
		print(self.mode)
	else
		self.mode = 0
		self.Owner:PrintMessage(HUD_PRINTCENTER, "Ammo Type: Bullets")
		print(self.mode)
	
end
end
end


It was really easy but I spent few hours reading it and reading again XD