How can I fix this attach_silencer command?

Here’s the code




function SWEP:SecondaryAttack()
	if self.Owner:KeyDown(IN_USE) then
		if self.Silenced == true then
			self.Weapon:SendWeaponAnim(ACT_VM_DETACH_SILENCER)
			self.Silenced = false
			self:SetNextPrimaryFire(CurTime() + 2)
			self:SetNextSecondaryFire(CurTime() + 2)
			self.ReloadDelay = CurTime() + 2
		else
			self.Weapon:SendWeaponAnim(ACT_VM_ATTACH_SILENCER)
			self.Silenced = true
			self:SetNextPrimaryFire(CurTime() + 2)
			self:SetNextSecondaryFire(CurTime() + 2)
			self.ReloadDelay = CurTime() + 2
		end
	end
end


The problems are that it stops the weapon from being able to use ironsights at all, and even if the silencer appears to be on after the ACT_VM_ATTACH_SILENCER animation and the silencer is on the gun, using primary fire causes it to disappear and play the regular shooting sound.
How do I fix this?