Automatic Gun Fire Loop Problem

I’ve made a script for an automatic gun:
However, if I double click and hold down the mouse button on the second click it’ll fire faster than the cooldown time.
Here is my script:

Mouse.Button1Down:Connect(function()
	if Equipped == true then
		MouseB1Down = true
		if Aiming == true and Reloading.Value == false then
			if Automatic.Value == true then
				repeat
					if Equipped == true and Reloading.Value == false then
						FireGun()
						wait(FireCooldown.Value)
					else
						break
					end
					wait()
				until MouseB1Down == false or Equipped == false
			else
				if Equipped == true then
					FireGun()
				end
			end
		end
	end
end)

Mouse.Button1Up:Connect(function()
	if Equipped == true then
		MouseB1Down = false
	end
end)

You need to set a variable outside of the loop, you can name it whatever you want, but make sure that before starting a new loop it checks if that variable is already true

if Automatic.Value == true then

After that line add the check and make sure you change

				if Equipped == true then
					FireGun()
				end

to

				if Equipped == true and not variableNameHere then
					FireGun()
				end

The reason this bug is happening is because you don’t check if a loop is happening prior, hope this helps.

1 Like