Tool won't stop shooting when I tell it to "Reload"!

I’m trying to make a system that when I reach a certain amount of bullets, the tool has a cooldown after a few seconds, the ammo reaches the default amount.

The problem is that if I click rapidly while reloading, then the ammo value is basically “infinite” until I stop clicking! Does anyone has a solve to this?

Note: The Ammo value is a INT Value!

Local Script:

mouse.Button1Down:Connect(function()
	if tool.Parent.Name == plr.Character.Name then
		Active = true
		if CurrentBullets > 0 then
			while Active do
				if CurrentBullets > 0 and Reloading == false then
					if Reloading == false then
						print("IsShooting")
						event:FireServer(m.Hit.Position,chr)

						CurrentBullets -= 1
					else
						Reload()
					end
				end
				
				wait(0.1)
			end
		else
			Reload()
		end
	end
end)

do while Active and CurrentBullets > 0 do

Just a few suggestions. Handle more logic on the server side, dont let the client to simply decide if they have enough or not bullets to keep shooting. Reduce the bullets Int value from server script and from server check if they have enough to keep firing or not.
This is just a basic example but I dont recommend this I just edited a little your script:

local mouse = plr:GetMouse()
local Active = false

-- release the button
mouse.Button1Up:Connect(function()
	Active = false
end)

-- push the button
mouse.Button1Down:Connect(function()
	if tool.Parent.Name == plr.Character.Name then
		Active = true
		if CurrentBullets > 0 then
			while Active and CurrentBullets > 0 do
				if CurrentBullets > 0 and Reloading == false then
					if Reloading == false then
						print("IsShooting")
						event:FireServer(m.Hit.Position,chr) -- add sanity checks on the server side to allow the player to actually shoot a bullet if they has enough or deny if its zero

						CurrentBullets -= 1 -- DO NOT REDUCE THE BULLETS FROM CLIENT, DO IT ON SERVER SIDE
					else
						Reload()
					end
				end
				task.wait(0.1)
			end
		else
			Reload()
		end
	end
end)
1 Like

Sadly nothing changed, and i still don’t understand why

What changes you made? you went for the server side validation approach? or you just tried that ugly script that I sent?

The ugly script you sent (jk).

1 Like

I could test that approach (the ugly script), which I prefer not, that would be intended just to handle client animations/visuals stuff.

I would insist on changing the approach to validate if the player can shoot or not on a server side, otherwise its a gift for exploiters to do whatever they want.

1 Like

Ok, I’ll try to switch the sides from client to server-side.