Tri-Burst Pistol

hello, I am currently trying to create a replica of the m93 raffica from MW2, and need help on the 3-round burst part.
I’m fairly new to coding, and I’ve been on the idea for how to make the tri-burst for more than 2 hours.
And I cant seem to think of how to create this; Can someone help out?

You would have to use a Think hook to refire the weapon like this (untested):


SWEP.Primary.Cooldown = 0.1 -- How much time until the next burst round can be fired

SWEP.BurstInterval = 0.1 -- Time between each burst shot
SWEP.BurstCount = 2 -- How many burst shots to fire

function SWEP:SetupDataTables()
	self:NetworkVar("Bool", 0, "DoBurst")
	self:NetworkVar("Float", 0, "NextBurstFire")
	self:NetworkVar("Int", 0, "BurstCount")
end

function SWEP:Shoot()
	-- Shoot stuff here
end

function SWEP:PrimaryAttack()
	if (self:GetDoBurst()) then
		return
	end
	
	self:Shoot()
	
	self:SetDoBurst(true)
	self:SetNextBurstFire(CurTime() + self.BurstInterval)
	self:SetBurstCount(0)
end

function SWEP:Think()
	if (self:GetDoBurst()) then
		local flCurTime = CurTime()
		
		if (self:GetNextBurstFire() <= flCurTime) then
			self:Shoot()
			
			local iNewBurstCount = self:GetBurstCount() + 1
			
			if (iNewBurstCount == self.BurstCount) then
				self:SetDoBurst(false)
				self:SetNextPrimaryFire(flCurTime + self.Primary.Cooldown)
			else
				self:SetNextBurstFire(flCurTime + self.BurstInterval)
				self:SetBurstCount(iNewBurstCount)
			end
		end
	end
end

thank you for the reply, I’ll try to incorporate this into the code and work with it, I’ll give feedback after I’ve added and tested it


SWEP.Primary.Cooldown = 0.1

SWEP.BurstInterval = 0.1
SWEP.BurstCount = 2

function SWEP:SetupDataTables()
	self:NetworkVar("Bool", 0, "DoBurst")
	self:NetworkVar("Float", 0, "NextBurstFire")
	self:NetworkVar("Int", 0, "BurstCount")
end

function SWEP:PrimaryAttack()
	self:SetClip1(math.Clamp(self:Clip1() - 1, 0, 21))
	self.Weapon:EmitSound("weapons/m93Shoot.wav")
	if (self:GetDoBurst()) then
		return
	end
	
	local bullet = {}
		bullet.Num = 1
		bullet.Src = self.Owner:GetShootPos()
		bullet.Dir = self.Owner:GetAimVector()
		bullet.Spread = 0.225
		bullet.Tracer = 1
		bullet.Damage = self.Primary.Damage
		bullet.Force = 15
		bullet.AmmoType = "Pistol"
		self.Owner:FireBullets( bullet ) 
		
	self:SetDoBurst(true)
	self:SetNextBurstFire(CurTime() + self.BurstInterval)
	self:SetBurstCount(0)
end

function SWEP:Think()
	if (self:GetDoBurst()) then
		local flCurTime = CurTime()
		
		if (self:GetNextBurstFire() <= flCurTime) then
			self:PrimaryAttack()
			
			local iNewBurstCount = self:GetBurstCount() + 1
			
			if (iNewBurstCount == self.BurstCount) then
				self:SetDoBurst(false)
				self:SetNextPrimaryFire(flCurTime + self.Primary.Cooldown)
			else
				self:SetNextBurstFire(flCurTime + self.BurstInterval)
				self:SetBurstCount(iNewBurstCount)
			end
		end
	end
end

function SWEP:SecondaryAttack() return end


thats the current added on code, however, it makes the noise for shooting 3 shots, but only fires 1.
it also aims down sights for some reason
also, you can infinitely shoot, and you will never lose ammo. your clip will die down until it hits 0, then it will continue to fire if you try to primary attack, it wont make you reload or even take from stock ammo.

You have quite a few issues: I created Shoot and PrimaryAttack as separate methods for the burst to work properly, which you erased, thus the burst can never be called. Also, you have your SetClip and EmitSound calls above the GetDoBurst check, anyway, which is why it “shoots” infinitely. You are also never doing any initial checks for ammo in PrimaryAttack. Lastly, don’t use self.Weapon, and use self:GetOwner() instead of self.Owner.

[editline]19th July 2017[/editline]

Also, your SecondaryAttack doesn’t need to have a return. It’s an empty method.

Ok, looking through other code, I found code and managed to get it to work,

the sound technically doesnt work, but i used a tri-burst sound effect so it would sync up, anyways, the gun works, code works great!

was having a bit of trouble, but worked it out

ty for trying to help.

for anyone who might need the code and finds this thread, the code i used was


SWEP.FireMode = 0


function SWEP:PrimaryAttack()

	if ( !self:CanPrimaryAttack() ) then return end
	if ( self:Clip1() < 3 && self:Ammo1() > 0 && self.FireMode == 1 ) then
		self:Reload()
		return
	end

	if (self.FireMode == 1 || self:Clip1() < 3 ) then
		self:ShootBullet( 42, 1, 0.055 )
		self.Weapon:SetNextPrimaryFire( CurTime() + 0.1 )
		self.Owner:EmitSound("weapons/raffv2.wav")
		self:TakePrimaryAmmo( 1 )
      else
		self:ShootBullet( 42, 1, 0.015 )
		timer.Create("FireBurstShot1" .. tostring(self.Owner),0.08,1, function() BurstFire(self,42,1,0.02) end)
		timer.Create("FireBurstShot2" .. tostring(self.Owner),0.16,1, function() BurstFire(self,42,1,0.01) end)
		self.Weapon:SetNextPrimaryFire( CurTime() + 0.51 )
		self.Owner:EmitSound("weapons/daytri.wav")
		self:TakePrimaryAmmo( 1 )
       end	
end

function BurstFire( self, damage, number, accuracy)
	if ( !self:CanPrimaryAttack() ) then return end
	self:ShootBullet( damage, number, accuracy )
	self:TakePrimaryAmmo( 1 )
end

function SWEP:SecondaryAttack() return end

you will have to make the shootsound for where it says daytri a sound that is a tripple burst itself, idk if you can change it, but its default is a tripple burst
raffv2 is a single shot sound for if the guns clip1 falls below 3 aka the tripple burst ammo take.
anyways, just sync up your custom gunshot with the bullet shots and you’ll have your base tri-burst system :slight_smile:

Do not use timers for burst fire! It ruins prediction.

well fuck, everything works, why not use this one?

Read this page here: https://wiki.garrysmod.com/page/Prediction

sigh, guess i took the easy way, i’ll see if it pays off, or if i’ll need to rework the code entirely, and if i do, i might come crawling back here.