Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I add a cooldown to my ability?

This ability is based on the keybind R in case you need to know that. This ability can continuously be spammed upon clicking R instead of waiting, lets say about 3 seconds, before being able to be used again. Is there a way I can add a cooldown? my code:

local tweenService = game:GetService("TweenService")

game.ReplicatedStorage.AirMagic.WindPushRE.OnServerEvent:Connect(function(player)
	local character = player.Character
	local properties = {
		Size = Vector3.new(60 ,60, 60)
	}
	local newForceField = game.ServerScriptService["Wind Magic"].WindPush.WPush:Clone()
	newForceField.Parent = game.Workspace
	newForceField.CFrame = character.HumanoidRootPart.CFrame

	local info = TweenInfo.new(0.9, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)

	local defaultWalkSpeed = character.Humanoid.WalkSpeed
	local defaultJumpPower = character.Humanoid.JumpPower
	character.Humanoid.WalkSpeed = 0
	character.Humanoid.JumpPower = 0
	character.Humanoid.MaxHealth = 100
	character.Humanoid.Health = 100

	local outTween = tweenService:Create(newForceField, info, properties)
	outTween:Play()

	wait(1)

	local properties2 = {
		Transparency = 0.75;
	}
	
	wait(1)

	local properties2 = {
		Transparency = 0.8;
	}
	
	wait(1)

	local properties2 = {
		Transparency = 0.9;
	}

	local info2 = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.In)
	local fadeTween = tweenService:Create(newForceField, info2, properties2)
	fadeTween:Play()
	wait(3)
	character.Humanoid.WalkSpeed = 16
	character.Humanoid.JumpPower = 50
	character.Humanoid.MaxHealth = 100
	character.Humanoid.Health = 100
	newForceField:Destroy()
end)

1 answer

✓ Accepted answer

I think your looking for Debounce, Debounce Is the cooldown

Example Script:

local Part = script.Parent
local Debounce = false

Part.Touched:Connect(function(hit)
	if not Debounce then
		local Humanoid = hit.Parent:WaitForChild("Humanoid")
		if Humanoid then
			Debounce = true
			print("Hey! Stop stepping on me! >:(")
			wait(1)
			Debounce = false
		end
	end
end)

2 comments

0

thank you! I got confused since I managed to make a cooldown system without using Debounce and when I tried to add it to the current code it did not work but it does work on other code. I might have set it up wrong there. at least I can do it the proper way now too.

0

No problem, always happy to help

This discussion is preserved as it was. New answers, comments, and votes are closed.