How do I make a gun fire in bursts instead of auto

I’m currently trying to make my gun fire in bursts, but all I’ve tried resulted in failure

if doesshoot == 4 then
	if script.Parent:FindFirstChild("Humanoid").Health == 0 then
		death()
	end
	doesshoot = 0
	npc["Cannon"].shoot:Play()
	local bullet = Instance.new("Part", npc) -- creates bullet
	bullet.Size = Vector3.new(0.05,0.05,2)
	bullet.BrickColor = BrickColor.new("Persimmon")
	bullet.Material = "Neon"
	bullet.CFrame = npc["Cannon"].CFrame * CFrame.new(0,0,-4)
	bullet.CanCollide = false
	local velocity = Instance.new("BodyVelocity", bullet)
	velocity.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	bullet.CFrame = CFrame.new(script.Parent:FindFirstChild("Head").Position, victimhead.Position) -- controls where the enemy is aiming gun
	velocity.Velocity = bullet.CFrame.lookVector * 150 + Vector3.new(math.random(-10,10),math.random(-10,10),0)
	local pointlight = Instance.new("PointLight", npc["Cannon"])
	game.Debris:AddItem(pointlight,0.1)
	local function damage(part)
		local damage = 5
		if part.Parent.ClassName ~= "Accessory" and part.Parent.Parent.ClassName ~= "Accessory" and part.ClassName ~= "Accessory" and part.Parent.Name ~= npc.Name and part.CanCollide == true then
			bullet:destroy()
			local victimhumanoid = part.Parent:findFirstChildOfClass("Humanoid")
			if victimhumanoid then
				if victimhumanoid.Health > damage then
													victimhumanoid:TakeDamage(damage)
				else
													victimhumanoid:TakeDamage(damage)
				end
			end
		end
	end
	game.Debris:AddItem(bullet, 5)
	bullet.Touched:connect(damage) --possibly damages enemy
								end

I’ve tried using a for loop, but even though it works, the enemy’s rotation when aiming is really choppy because of the wait(), and I belive that is caused by the line

velocity.Velocity = bullet.CFrame.lookVector * 150 + Vector3.new(math.random(-10,10),math.random(-10,10),0)

but I don’t know how to include both. Its either with the choppy rotation and bursting, or no bursting.

1 Like

Hey there! You can make your gun fire in bursts through a while loop, and a for loop nested inside.
It should look something like this:

--// Variables
local players = game:GetService('Players')
local player = players.LocalPlayer
local mouse = player:GetMouse()

local is_shooting = false
local bullet_burst_count = 5 --// How many bullets to shoot every burst.
local bullet_burst_cooldown = 0.25 --// How many seconds to wait between each intervertal.

--// Functions
local function shoot_bullet()
	--// Write your own bullet code here.
end

local function start()
	is_shooting = true
	while is_shooting do
		for i = 1, bullet_burst_count do task.wait(0.05) --// 0.05 seconds per bullet fired.
			shoot_bullet()
		end
	task.wait(bullet_burst_cooldown)
	end
end

local function stop()
	is_shooting = false
end

--// Events
mouse.Button1Down:Connect(function()
	start()
end)

mouse.Button1Up:Connect(function()
	stop()
end)

Hope this is what you were trying to achieve.
Happy coding!