How to make yourself more accurate when crouched



function SWEP:PrimaryAttack( num )

	if ( !self:CanPrimaryAttack() ) then return end
	
		firerate = 0.08
		sound = "weapons/M4a1/m4a1-1.wav"
		Accuracy = 0.05
		
		
		self.Weapon:EmitSound( sound )
		
		self:SetNextPrimaryFire( CurTime() + firerate )

		self:ShootBullet( 23, 1, Accuracy )

		self:TakePrimaryAmmo( 1 )
		
		self.Owner:ViewPunch( Angle( -0.6, math.Rand( -0.5, 0.5 ), math.Rand( -0.2, 0.2 ) ) )
	
				if ( key == IN_DUCK ) then
		
				firerate = 0.08
				sound = "weapons/M4a1/m4a1-1.wav"
				
				self.Weapon:EmitSound( sound )
				
				self:SetNextPrimaryFire( CurTime() + firerate )

				self:ShootBullet( 23, 1, Accuracy )

				self:TakePrimaryAmmo( 1 )
				
				self.Owner:ViewPunch( Angle( -0.6, math.Rand( -0.5, 0.5 ), math.Rand( -0.2, 0.2 ) ) )

				Accuracy = 0.003
				
				hook.Add( "KeyPress", "crouch", function( ply, key ))
		
		end
	end
end


Trying to make it that its more accurate when crouched.
Any hints or any kind of help would be nice

This is one way you could do it


local Accuracy = 0.05
if self.Owner:Crouching() then Accuracy = 0.003 end

self:ShootBullet( 23, 1, Accuracy )

It changes accuracy but it doubles damage and bullets
Ironically I had a similar code in the start

It shouldn’t, post code in which it does. Also, use locals.


function SWEP:PrimaryAttack( num )

	if ( !self:CanPrimaryAttack() ) then return end
	
		local firerate = 0.08
		local sound = "weapons/M4a1/m4a1-1.wav"
		local Accuracy = 0.01
		local Damage = 23
		local bul_num = 1
		
		
		self.Weapon:EmitSound( sound )
		
		self:SetNextPrimaryFire( CurTime() + firerate )

		self:ShootBullet( Damage, bul_num, Accuracy )

		self:TakePrimaryAmmo( 1 )
		
		self.Owner:ViewPunch( Angle( -0.6, math.Rand( -0.5, 0.5 ), math.Rand( -0.2, 0.2 ) ) )
		
	if self.Owner:Crouching() then Accuracy = 0.007 end

	self:ShootBullet( Damage, bul_num, Accuracy )
	
end

Thats how I have it now

you are calling ShootBullet twice, thats why damage is doubled

Now it literally doesnt do anything rip

Post your new code.


function SWEP:PrimaryAttack( num )

	if ( !self:CanPrimaryAttack() ) then return end
	
		local firerate = 0.08
		local sound = "weapons/M4a1/m4a1-1.wav"
		local Accuracy = 0.1
		local Damage = 23
		local bul_num = 1
		
		
		self.Weapon:EmitSound( sound )
		
		self:SetNextPrimaryFire( CurTime() + firerate )

		self:ShootBullet( Damage, bul_num, Accuracy )

		self:TakePrimaryAmmo( 1 )
		
		self.Owner:ViewPunch( Angle( -0.6, math.Rand( -0.5, 0.5 ), math.Rand( -0.2, 0.2 ) ) )
		
	if self.Owner:Crouching() then Accuracy = 0.001 end
	
end

Why are you changing the accuracy when crouching AFTER everything has already executed?