How do I make this gun automatic?

I tried making this automatic but it won’t work. Can someone help me?

	m.Button1Down:connect(function()
		if equip_value == true or reloading or toggle == false or chamber.IsPlaying  then return end
		if ammo <= 0 or chambered == false then t.Parent.Events.Empty:FireServer() return end
		if not running then
		running = true;
		self = t.Parent.Parent;
		local ray = Ray.new((muzzle.CFrame.p + Vector3.new(0, 0, 0)), ((m.Hit.p - muzzle.CFrame.p).unit * 100))
		local hit, position = workspace:FindPartOnRay(ray, self);
		local spread_amount = math.random(-1.25,1.25)
		position = Vector3.new(position.X + (spread_amount * spread_amount * math.random()),position.Y + (spread_amount * spread_amount * math.random()),position.Z + (spread_amount * spread_amount * math.random()))	
		pcall(function()
		if hit.Parent.Humanoid then
			t.Parent.Events.Damage:FireServer(hit, damage);
			end
			end);
			shoot:Play()
			t.Parent.Events.Laser:InvokeServer(self, position, muzzle);
			t.Parent.Events.Shoot:FireServer()
			ammo-=1
			if ammo == 0 and reserve == 0 then
				game.Players.LocalPlayer.PlayerGui.ammoGui.Chamber.Text = "NOT CHAMBERED"
			end
			game.Players.LocalPlayer.PlayerGui.ammoGui.Ammo.Text = ammo
			t.Parent.Events.Bullet:FireServer()	
			ShootRecoil()
			print(ammo)
			wait(cool - .1);
			running = false;
		end;
	end)
end)```
1 Like

What do you mean automatic? Like when you tap once the gun still shoots?

1 Like

Try making a bool value and then when the player pushes down the left click, set it to true, and then make a while (value is true) statement and put the shooting code inside that (remember to put a wait with the fire rate inside that wait after the shooting is done otherwise it will break the script). And when the player releases their left click, the bool value is set to false, and the loop breaks. Also put the while loop inside the event of the button 1 down otherwise it will skip the while loop.

1 Like
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local FIRE_KEY = Enum.UserInputType.MouseButton1

local function fire()
    ...
end

RunService.RenderStepped:Connect(function()
    if UserInputService:IsMouseButtonPressed(FIRE_KEY) then
        fire()
    end
end

You also might want to have a cool down if you don’t have that in your code already.

local RPM = 660
local LastFired = tick()

local function fire()
    if (tick() - LastFired) < (1 /(RPM / 60)) then
        print("firing 2 quickly")
        return
    end

    ...

    LastFired = tick()
end

This prevents your ordinary players from doing remote spams by holding MouseButton1.

2 Likes