While loop problem

I am creating a gun but now i have a problem where when i hold left click it fires automatically but it goes over the magazine count like 30 / 30 to -1 / 30 instead of reloading and it will only reload when i let go and try to fire again

it appears to me that it is a problem with the while loop but i do not know how i can fix this

-- 
function fireLoop()
	if ammo > 0 and not reloading then
			while firing do
				script.Parent.RemoteEvent:FireServer(mouse.Hit.p)
				recoil:Play()
				ammo = ammo - 1
				task.wait(.08)
		end
			elseif reloading == false then
			reload()
		end
	while wait() do
		if equipped == true then
			ammocount.Visible = true
			ammocount.Text = "AMMO: "..(ammo).." | "..maxammo
		end
	end
end
function fireLoop()
	if ammo > 0 and not reloading then
		while firing and ammo > 0 do
			script.Parent.RemoteEvent:FireServer(mouse.Hit.p)
			recoil:Play()
			ammo = ammo - 1
			task.wait(.08)
		end
		
		if not reloading and ammo <= 0 then
			reload()
		end
		
	end	
end




   -- put this while loop at bottom of script
	while wait() do
		if equipped == true then
			ammocount.Visible = true
			ammocount.Text = "AMMO: "..(ammo).." | "..maxammo
		end
	end

You never check the ammo value during the loop, you only check it in the if statement, code should look something like this

Also made it to where it reloads automatically after you let go of mouse

1 Like

If the ammo has become a non-integer value like .000001 then it’ll fire and become -.99999. Maybe change it to while firing and ammo >=1 so that it only subtracts 1 more from the value if it’s 1 or more.

Never had this issue in Roblox Lua when using variables (this problem can occur when dealing with humanoids and infact in the old days of roblox scripters would check health < 0.001, although this been fixed), am aware the issue exist in other languages.

You should private message @Bug-Support with the appropriate details of a bug report as outlined in this thread: How to post a Bug Report. *Only Regulars can create bug reports so this isn’t a forum bug