How would i add a cooldown to this script?

local tool = script.Parent
local da = true
tool.Touched:Connect(function(OtherPart)
	if OtherPart.Parent:FindFirstChild("Humanoid") and da == true then
		da = false
		local player = game.Players:GetPlayerFromCharacter(OtherPart.Parent)
		OtherPart.Parent.Humanoid:TakeDamage(50)
		wait(0.5)
		script.Enabled = false
		wait(2)
		script.Enabled = true
		da = true
		if OtherPart.Parent:WaitForChild("Humanoid",5).Health == 0 then
			print("test")
		end
	end
end)
local tool = script.Parent
local cooldown = false
local debounce = 3 -- THE NUMBER OF SECONDS IN YOUR COOLDOWN

tool.Touched:Connect(function(OtherPart)
	if OtherPart.Parent:FindFirstChild("Humanoid") and cooldown == false then
		cooldown = true
		local player = game.Players:GetPlayerFromCharacter(OtherPart.Parent)
		OtherPart.Parent.Humanoid:TakeDamage(50)
		task.wait(debounce)
		cooldown = false
		if OtherPart.Parent:WaitForChild("Humanoid",5).Health == 0 then
			print("test")
		end
	end
end)
1 Like

Sorry for not telling but what im trying to also achieve is like. The Script to be disabled until i click again. how would i do that?

Do you want the function to run if the player clicks with the tool equipped, or if they touch another player?

If the player clicks when the tool is equipped.

Btw im trying to achieve that because when i click. the script does damage. but waits 3 seconds and it does damage with no clicking.