How do I fix my gun?

Hello everyone. Recently I made a debounce for my gun this debounce was made for a shooting cooldown because you could just spam click. I managed to make the debounce but then I had a problem. I also had a gui that shows how much ammo you have left until you have to reload. The gui was supposed to change the value down by 1 the problem is that whenever I shoot my gun the gui/ammo counter would reset back to 10.
Here is a example:
Gun gui

And this is my current main script:
Debounce = true
local maxAmmo = 10
local Ammo = maxAmmo
local reloading = false
local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild(“PlayerGui”)
local textLabel = playerGui:WaitForChild(“AmmoDisplay”):FindFirstChild(“AmmoText”)

script.Parent.Equipped:Connect(function(Mouse)
local function reload()
reloading = true
wait(1)
Ammo = maxAmmo
reloading = false
end

script.Parent.Activated:Connect(function()
	if Ammo > 0 and not reloading then
		Ammo = Ammo - 1
		script.Parent.GunShot:Play()
		if Debounce == true then
			Debounce = false
			if Mouse.Target.Parent:FindFirstChild("Humanoid") then
				script.Parent.Fire:FireServer(Mouse.Target.Parent, 35)
				wait(1)
				Debounce = true
			end
		elseif reloading == false then
			reload()
			script.Parent.GunShot:Stop()
			while wait() do
				textLabel.Text = (Ammo).." / "..maxAmmo
			end
		end
	end
	local Input = game:GetService("UserInputService")
	Input.InputBegan:Connect(function(Key)
		if Key.KeyCode == Enum.KeyCode.R and reloading == false and Ammo ~= maxAmmo then
			reload()
		end
	end)
end)

end)

Can anyone help?
Thanks!

There is a couple of problems here so I’ll try to explain as many as I can here:

First of all, the scope of your event connections can and will cause problems, in your code, whenever “script.Parent” is equipped, the code inside .Equipped is ran, meaning for each time the user equips their gun, another .Activated connection will be made.

To put this in a gameplay perspective, if I were to equip the gun 5 times, and then click once, the .Activated code would fire 5 times simultaneously, as a connection has been made for each time I’ve equipped the gun.

This also applies to the InputBegan event. Whilst it may not be the entire source of your problem, it’s something to fix.

Secondly you’re deducting ammo regardless of the debounce, checking the debounce after deducting ammo and playing the gunshot sound.

Your debounce code should be similar to this:

	if not Debounce then
		Debounce = true
		
		--Code you want to run here
		
		task.wait(1)
		--Set debounce to false so the code can run again
		Debounce = false
	end

Essentially if there isn’t a debounce active, set the debounce to active, let the code run, wait a second, and set the debounce to not active again so code can run.

Another thing, although not fundamental to your code running, but good to know, I presume you’re passing through the instance that’s been hit(Mouse.Target.Parent) and the damage to deal.

This can be exploited very easily, allowing for people to make kill all scripts, deal extra damage, basically just abuse the remote event.

Inside this snippet:

elseif reloading == false then
			reload()
			script.Parent.GunShot:Stop()
			while wait() do
				textLabel.Text = (Ammo).." / "..maxAmmo
			end
		end

You have ( i believe ) a loop that’s going to yield your code, the while wait() do loop has no exit conditions and hence is going to run forever. Since it’s running on the same thread it will yield your existing code.

I apologize that this isn’t one simple fix for your issue but maybe fixing these things could help you identify the problem easier, it also will surely help prevent against further issues down the road.

textLabel.Text = (Ammo).." / "..maxAmmo
why have “Ammo” in brackets? i don’t understand, also maybe it’s not updating, you could try using textLabel.Text = tostring(Ammo.." / "..maxAmmo)