Issues with "Script timeout exhausted allowed execution time"

So I’m having an issue where I’m making a fully automatic firearm and I would like it to begin a loop of shooting when the mouse is pressed down, and when it is released, it stops. However there seems to be some issues with it when I use it.

There is an error message saying “Script timeout exhausted allowed execution time” and it says the issues is at the line of the while loop. Here is the code:

connection = mouse.Button1Down:Connect(function()
	if not mouseDown then
		mouseDown = true
	end
	while mouseDown do
		if configs.Ammo.Value > 0 then
			mouseClick:FireServer()
			wait(60/Firerate)
		end
    end
end)
connection2 = mouse.Button1Up:Connect(function()
	mouseDown = false
end)

Basically this loop is causing an error and I have no idea why. I can provide more information on the issue if needed. (also the connections are variables because this is occurring inside a function in which the mouse is defined so it must be connected in here)

You’ll need to add a wait at the end of the loop

So the wait inside the conditional does not count?

You’ll need to add one outside of the if statement inside the loop.

oh ok, thanks I will try that and see if it works

Alright, it did work! thanks for all the help