Debounce on Gun Isn't Working

I made this gun for my game and everything works except you can easily just spam click and deal tones of damage, your only supposed to be able to shoot every .15 seconds, here’s what I mean:

Holding down the mouse:

Spam clicking:

Here’s my script, its quite long so I removed a lot of unnecessary stuff to make it less confusing and show the actual important stuff. Any help would be appreciated, Merry Christmas :pray:

local tool = script.Parent
local fireRate = 1.5

local shooting = false
local shootDb = true

tool.Equipped:Connect(function(mouse)
	
	tool.Activated:Connect(function()
		if shootDb == true then
			shootDb = false
			shooting = true
				
			while shooting do
				if shootDb == false then
					-- gun stuff
				end	
				
				wait(fireRate)
			end
		end	
	end)
	
	tool.Deactivated:Connect(function()
		shooting = false
		wait(fireRate)
		shootDb = true
	end)
	
	tool.Unequipped:Connect(function()
		shooting = false
		shootDb = true
	end)
end)
1 Like

do you break the while loop if shooting is false?

Just added that in, still doesnt work though

while shooting do
	if shootDb == false then
		-- gun stuff
    else
        break
	end				
	wait(fireRate)
end

Try this:

local tool = script.Parent
local fireRate = 1.5

local shooting = false
local shootDb = false

tool.Equipped:Connect(function(mouse)

tool.Activated:Connect(function()
    shooting = true
	while shooting and not shootDb do
		if shootDb == false then
			-- gun stuff
		end	
		shootDb = true
		wait(fireRate)
        shootDb = false
	end
end)

tool.Deactivated:Connect(function()
	shooting = false
end)

tool.Unequipped:Connect(function()
	shooting = false
end)

end)

2 Likes

Oh thanks! For a minute this didn’t work because I forgot the remove the shootDb = true in tool.Unequipped and tool.Deactivated. Thank you I’ve been trying to fix this for a while.

1 Like