Debounce / Cooldown not working?

I wanted to add a cooldown to the firing of the gun I was making, but the cooldown doesn’t work…
I’ve looked at some possible solutions, but none work. It might have something to do with the remote event or it being a while true loop, but I can’t think of anything that could fix it.

(localscript in gun)

local tool = script.Parent.Parent

local shots = 0

local mouse = game.Players.LocalPlayer:GetMouse()

local firing = false

local replicatedstorage = game:GetService("ReplicatedStorage")

local remoteevent = replicatedstorage:WaitForChild("BallistaFire")

local spin = 0

local humanoider = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")

tool.Equipped:Connect(function(mouse)
	local player = game.Players.LocalPlayer
	mouse.Button1Down:Connect(function()
		firing = true
		while firing do
			firing = false
			script.Parent.Shoot_Sound:Play()
			remoteevent:FireServer(tool.Handle.Position , mouse.Hit.p)
			wait(5)
			firing = true
		end
	end)
	mouse.Button1Up:Connect(function()
		
	end)
end)

(serverscript in serverscriptservice)

local replicatedstorage = game:GetService("ReplicatedStorage")

local remoteevent = replicatedstorage:WaitForChild("BallistaFire")

remoteevent.OnServerEvent:Connect(function(player, gunPos, mousePos)
	
	local bullet = replicatedstorage:FindFirstChild("Bullet")
	local bulletclone = bullet:Clone()
	bulletclone.Parent = game.Workspace
	
	local speed = 500
	bulletclone.CFrame = CFrame.new(mousePos)
	
	bulletclone.Touched:Connect(function(hit)
		local humanoidhit = hit.Parent:FindFirstChild("Humanoid")
		if humanoidhit ~= nil then
			if hit.Parent.Name ~= player.Name then
				humanoidhit:TakeDamage(100)
				bulletclone:Destroy()
			end
		end
	end)
	
end)
1 Like
local tool = script.Parent.Parent

local shots = 0

local mouse = game.Players.LocalPlayer:GetMouse()

local firing = false

local replicatedstorage = game:GetService("ReplicatedStorage")

local remoteevent = replicatedstorage:WaitForChild("BallistaFire")

local spin = 0

local humanoider = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")

tool.Equipped:Connect(function(mouse)
	local player = game.Players.LocalPlayer
	mouse.Button1Down:Connect(function()
		firing = true
		while firing == true do
			firing = false
			script.Parent.Shoot_Sound:Play()
			remoteevent:FireServer(tool.Handle.Position , mouse.Hit.p)
			wait(5)
			firing = true
		end
	end)
	mouse.Button1Up:Connect(function()
		
	end)
end)

Try this in the local script and see if it works!

mouse.Button1Up:Connect(function()

Mouse.Button1Up is a risky thing to use i recommend using user input service because there are many ways to bypass reloads if this is in use i believe from my experience

Try to see if this works, and did you get any errors