Help with making an automatic gun

I am making a gun but I am having trouble with making it automatic (machine-gun-like).

The problem is that when I hold down click it doesn’t constantly shoot or shoot at all.

This is my code:

local mouse = game.Players.LocalPlayer:GetMouse()
local uis = game:GetService("UserInputService")
local maxammo = 10
local ammo = 10
local tool = script.Parent
local equipped = nil
local clicked = false
--Mouse clicked
mouse.Button1Down:Connect(function()
	clicked = true
end)
--Mouse un-clicked
mouse.Button1Up:Connect(function()
	clicked = false
end)
--Tool equipped
tool.Equipped:Connect(function()
	equipped = true
end)
--Tool unequipped
tool.Unequipped:Connect(function()
	equipped = false
end)
if equipped then
	if clicked then
		if ammo > 0  then
			script.Parent.fire:InvokeServer(mouse.Hit.Position) --Tells the server to shoot the bullet
			ammo -= 1
			wait(0.3)
		end
	end
end
--This next bit is not important but i will show it anyway
uis.InputBegan:Connect(function(input)
	if input == Enum.KeyCode.Return  then
		ammo = maxammo

	end
end)

As you can see, i have made a two functions that check if the gun is equipped and I have another two that will tell the shoot function if the mouse button is clicked.

Thanks in advance!

I think you should use while loop on this part

if equipped then
	if clicked then
		if ammo > 0  then
			script.Parent.fire:InvokeServer(mouse.Hit.Position) --Tells the server to shoot the bullet
			ammo -= 1
			wait(0.3)
		end
	end
end

because it will only fire one time when the script is running

1 Like