Gun firing more than once when mouse is clicked on burst taps

When fired in burst taps, the gun fires more than one bullet and I have no idea why. I have tried everything but yet it still fails.
Here is the code.

Mouse.Button1Down:connect(function()
	if aiming == true then
		if debounce2 == false then
			debounce2 = true
			if roundsinclip > 0 then
				shooting = true
				while shooting do
					if shootingdebounce == false then
						game.ReplicatedStorage.gunsystem.shotsound:FireServer(emitter, "shot")
						roundsinclip = roundsinclip - 1
						shootingdebounce = true
						wait(0.2)
						shootingdebounce = false
						roundsui.Text = roundsinclip
						shootanimation:Play()
					end
				end
			else
				game.ReplicatedStorage.gunsystem.shotsound:FireServer(emitter, "empty")
				shooting = false
			end
		end
	end
end)

Here is a video of it happening.

Mouse.Button1Down:connect(function()
	if aiming == true then
		if debounce2 == false then
			debounce2 = true
			if roundsinclip > 0 then
				shooting = true
				while shooting do
					if shootingdebounce == false then
						game.ReplicatedStorage.gunsystem.shotsound:FireServer(emitter, "shot")
						roundsinclip = roundsinclip - 1
						shootingdebounce = true
						wait(0.2)
						roundsui.Text = roundsinclip
						shootanimation:Play()
                                                shootingdebounce = false
					end
				end
			else
				game.ReplicatedStorage.gunsystem.shotsound:FireServer(emitter, "empty")
				shooting = false
			end
		end
	end
end)

[

I think you must disable debounce after some deeper code.

I’ve tried it with and without debounce and it still happens.

From what I’ve seen in this script when Button1Down function runs it spawns a loop that fire your guns and the problem is that these loop do stacks together so I implemented a variable that detect if the loop is running or not. The loop detection detects if any of the firing function loop exist, if it does it not run the loop again.

local LoopExisted = false

Mouse.Button1Down:connect(function()
    if aiming == true then
    	if debounce2 == false then
    		debounce2 = true
    		if roundsinclip > 0 then
				shooting = true
				LoopExisted = true
    			while shooting and LoopExisted do
    				if shootingdebounce == false then
    					game.ReplicatedStorage.gunsystem.shotsound:FireServer(emitter, "shot")
    					roundsinclip = roundsinclip - 1
    					shootingdebounce = true
    					wait(0.2)
    					shootingdebounce = false
    					roundsui.Text = roundsinclip
    					shootanimation:Play()
    				end
				end
				LoopExisted = false
    		else
    			game.ReplicatedStorage.gunsystem.shotsound:FireServer(emitter, "empty")
    			shooting = false
    		end
    	end
    end
end)

You need to move your shooting debounce above higher before the ammo is taken away and the remote is fired preferably right after you check if it is false always make the debounce the first thing to change

Mouse.Button1Down:connect(function()
	if aiming == true then
		if debounce2 == false then
			debounce2 = true
			if roundsinclip > 0 then
				shooting = true
				while shooting do
					if shootingdebounce == false then
						shootingdebounce = true
game.ReplicatedStorage.gunsystem.shotsound:FireServer(emitter, "shot")
						roundsinclip = roundsinclip - 1
						wait(0.2)
						shootingdebounce = false
						roundsui.Text = roundsinclip
						shootanimation:Play()
					end
				end
			else
				game.ReplicatedStorage.gunsystem.shotsound:FireServer(emitter, "empty")
				shooting = false
			end
		end
	end
end)

After

while shooting do

Add a

wait(.05)

If it still does not work, try making the wait longer

1 Like

Still doesn’t work.
(30 characters)

try to add the debounce at the end

This might of actually worked, give me two seconds.

1 Like

No problem! Let me know if you need anything else!