How do i Break this Loop

Hello Roblox Community,
I recently started to learn scripting by fiddling around with scripts i find and i am working on this weapon script and modificating it and i have a problem

So this gun script has a jamming feature however the jam feature gets only triggered when the player presses the mousebutton down not per shot from the gun so i took
the code wich made it jamable by chance and put it into the gun fired and that also works
however the gun keeps shooting even tho the jammed variable gets changed into “true”
i want it to stop shooting once the variable jammed gets changed to true

i currently also use the commentary way to “disable” some lines of code wich i copied and put somewhere else so i dont have to return or incase i mess up

Mouse.Button1Down:connect(function()
	Down = true
	local IsChargedShot = false
	if Equipped and Enabled and Down and not Jammed and not Reloading and Ammo > 0 and Humanoid.Health > 0 then
		Enabled = false
		--if not Jammed then
			--if Chance(Module.JamChance) == true then
				--Jammed = true
			--end
		--end
		if Module.ChargedShotEnabled then
			if HandleToFire:FindFirstChild("ChargeSound") then HandleToFire.ChargeSound:Play() end
			wait(Module.ChargingTime)
			IsChargedShot = true
		end
		if Module.MinigunEnabled then
			if HandleToFire:FindFirstChild("WindUp") then HandleToFire.WindUp:Play() end
			wait(Module.DelayBeforeFiring)
		end
		while Equipped and not Reloading and not PullingOut and (Down or IsChargedShot) and Ammo > 0 and Humanoid.Health > 0 do
			IsChargedShot = false
			for i = 1,(Module.BurstFireEnabled and Module.BulletPerBurst or 1) do
				spawn(ShakeCamera)
				for x = 1,(Module.ShotgunEnabled and Module.BulletPerShot or 1) do
					Fire(HandleToFire)
				end
				Ammo = Ammo - 1
				ChangeAmmoAndClip:FireServer(Ammo,Clips)
				UpdateGUI()
				script.Parent.Bolt.Transparency = 1
				script.Parent.BoltBack.Transparency = 0
				if Module.BurstFireEnabled then wait(Module.BurstRate) end
				wait(0.05)
				script.Parent.Bolt.Transparency = 0
				script.Parent.BoltBack.Transparency = 1
				if Ammo <= 0 then break end
				if Jammed then break end
			end
			HandleToFire = (HandleToFire == Handle and Module.DualEnabled) and Handle2 or Handle
			wait(Module.FireRate)
			if not Module.Auto then break end
		end
		if HandleToFire.FireSound.Playing and HandleToFire.FireSound.Looped then HandleToFire.FireSound:Stop() end
		if Module.MinigunEnabled then
			if HandleToFire:FindFirstChild("WindDown") then HandleToFire.WindDown:Play() end
			wait(Module.DelayAfterFiring)
		end
		ObjectEdit:FireServer(Handle, 'GunFiredFinal')
		Enabled = true
	elseif Equipped and Enabled and Down and not Reloading or Jammed and Ammo <= 0 and Humanoid.Health > 0 then 
		--Reload() 
		ObjectEdit:FireServer(Handle, 'GunEmpty')
	end
end)

Mouse.Button1Up:connect(function()
	Down = false
end)

Try using Repeat Until loop, It’s much much more comfortable for me personally and might solve your problem.

1 Like

You also want to try saying:
while shooting do
–shooting the bullet
end

Shooting is a Boolean variable.

what i dont understand is in the “while” function at the end it says
“if Ammo <= 0 then break end” does this mean once the person has 0 bullets the shooting should stop ? why it dosent work when i type “if Jammed then break end” ???

omg i just realized the mistake
i had to add “and not Jammed” to the “while” function now it works the way i want it to work

oh god lmao but thanks for anyone who tried to help