How to have a cool down time(r) for mouse.Move?

This is my first time doing a topic, so please excuse me if there are any errors. I know that mouse.Move will run when a player moves the mouse, I also would like to make cool down when, for example firing an object. So far with the code I have, I can fire blocks infinitely without cool down or time in between. --This is all activated by a tool–

Here is a video (if it works):
robloxapp-20200325-1828021.wmv (755.4 KB)

I have tried placing wait() in different places in code, no difference

local damage = 1
local speed = 150
local destroyTime = 5
local coolDown = 1

local mouseDown = false
local equipped = false

p.Equipped:Connect(function()
	equipped = true
	mouseDown = true
	mouse.Move:Connect(function()
		if equipped == true then
			if mouseDown == true then
				mouseDownFunction()	
			end
		end
	end)					
end)

p.Unequipped:Connect(function()
	equipped = false
	mouseDown = false
	mouseUpFunction()
end)

function mouseDownFunction()
	local Debounce = false
----------------------------------------------------------------------------------
	local clone = ship:Clone()
	clone.Parent = game.Workspace
	clone.Name = player.Character.Name.."Part"
	clone.Position = character.Position + Vector3.new(5,0,0)
----------------------------------------------------------------------------------	
    local bv = Instance.new("BodyVelocity")
   	bv.Parent = clone 
    bv.MaxForce = Vector3.new(1e8,1e8,1e8) 
   	bv.Velocity = (mouse.hit.p - character.Position).unit * speed
----------------------------------------------------------------------------------
	local sparkes = Instance.new("ParticleEmitter", clone)
	sparkes.Size = NumberSequence.new(5)
	clone.CanCollide = false	
------------------------------------------------------------------------------		
	clone.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and Debounce == false then 
			if hit.Parent ~= player.Character.Name then
				hit.Parent.Humanoid:TakeDamage(damage)
				wait(0.1)
				clone:Destroy()
			else
				wait(destroyTime)
				clone:Destroy()
			end
		else 
			wait(destroyTime)
			clone:Destroy()
		end
	end)	
-------------------------------------------------------------------------------			
	wait(destroyTime)
	clone:Destroy()
	wait(coolDown)
end

function mouseUpFunction()
	wait(destroyTime)
	for _, v in pairs(game.Workspace:GetChildren()) do
		if v.Name == player.Character.Name.."Part" then
			v:Destroy()
		end
	end
end

You can make your coolDown variable hold a boolean (true/false) instead of a number and just yield for the amount of time between firing right after. For example:

local canFire = true

-- let's say this function is called when they click
function onFire()
	if canFire then
		canFire = false

		-- fire

		wait(5) -- yield for 5 seconds so they can't fire for 5 seconds

		canFire = true
	end
end
1 Like

It just fires once and doesn’t do it again, i might forgotten but this is activated by a tool

1 Like

Did you define a reference for canFire above the scope of the onFire function? Also are you waiting 5 seconds before you try clicking again?

local canFire = true

local function onFire()
    if canFire then
        canFire = false

        wait(5)
        canFire = true
    end
end

If this doesn’t work try posting your current code.

The code is already posted above

I found the solution, thank you for the replies. The destoryTime was accumulating. And yes, soutenu, your code works, the destroyTime was adding up. Thanks for the Help!