How can I start a cooldown after a weapon is done doing whatever it's doing?

Hello There!

As the title says, I’m trying to find a way to make a weapon go on cooldown once it does it’s thing. An example of what I am trying to find is where I don’t want a sword to immediately go on cooldown when it’s used but want to make the sword go on cooldown once the wielder is done swinging the sword.

The easiest way is to add

wait() or task.wait()

after the weapon is done doing whatever it’s doing

1 Like

I’m going to assume you’re using roblox tools.

You can just add a debounce, and then wait n seconds and disable that debounce

local tool: Tool = script.Parent

local cooldown = 3
local isOnCooldown = false
tool.Activated:Connect(function()
   if isOnCooldown then return end
   isOnCooldown = true
   
   -- start weapon attack
   ...
   
   -- upon completing weapon attack
   task.wait(cooldown) -- waits n seconds
   isOnCooldown = false
end)
1 Like

Yeah, I am using Roblox tools, but how do I set this script up exactly?

1 Like

The instance hierarchy would look something like this in the explorer:

- MyTool: Tool
   - MyToolScript: LocalScript

You just have to add a localscript to your tool. This can be done by going to the explorer, hovering over your tool, hitting the plus button, and then on the dialog that pops up hit the localscript.

1 Like

Yeah, thank you. I already know though but thank you. But what I really mean is how do I edit your script so it works what I wanted it to do.

1 Like

Can you send what you currently have?

1 Like

Um… okay…?

So this is a Local Script within the tool:

--//Tool
local Tool = script.Parent
local timeModule = require(Tool:WaitForChild("TimeModule"))
--//Character
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
--//Functionality
local Debounce = false
local HasSetPosition = false

local CCE

Tool.Equipped:Connect(function()
	Tool.Shell.EquipSound:Play()
	Character["Right Arm"]:WaitForChild("ToolGrip").Part1 = Tool.Base
	
	local EquipAnim = Humanoid:WaitForChild("Animator"):LoadAnimation(Tool.Equip)
	EquipAnim:Play()
	task.wait(0.15)
	
	local IdleAnim = Humanoid:WaitForChild("Animator"):LoadAnimation(Tool.Idle)
	IdleAnim:Play()
	Tool.Unequipped:Connect(function()
		IdleAnim:Stop()
		Character["Right Arm"]:WaitForChild("ToolGrip").Part1 = nil
	end)
end)

Tool.Activated:Connect(function()
	if HasSetPosition == false and not Debounce then
		Debounce = true
		
		Tool.SetPositionEvent:FireServer()
		HasSetPosition = true
		
		task.wait(1)
		Debounce = false
	elseif HasSetPosition == true and not Debounce then
		Debounce = true
		
		local Camera = workspace.CurrentCamera
		local Lighting = game.Lighting
		
		CCE = Instance.new("ColorCorrectionEffect",Lighting)
		CCE.TintColor = Color3.fromRGB(89, 89, 89)
		CCE.Contrast = 30
		--Tweening
		local TweenGoal = {
			Contrast = 0
		}
		local Tweeninfo = TweenInfo.new(
			1,
			Enum.EasingStyle.Sine,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		local Tween = game:GetService("TweenService"):Create(CCE,Tweeninfo,TweenGoal)
		Tween:Play()
		
		--Tweening Camera
		local TweenGoal = {
			FieldOfView = 10
		}
		local Tweeninfo = TweenInfo.new(
			1,
			Enum.EasingStyle.Sine,
			Enum.EasingDirection.Out,
			0,
			true,
			0
		)
		local Tween = game:GetService("TweenService"):Create(Camera,Tweeninfo,TweenGoal)
		Tween:Play()

		Tool.ReverseTimeEvent:FireServer()
		HasSetPosition = false

	end
end)

And this is the server script in the tool:

--//Tool
local Tool = script.Parent
local timeModule = require(Tool:WaitForChild("TimeModule"))
--//Functionality
Tool.SetPositionEvent.OnServerEvent:Connect(function(Plr)
	local Char = Plr.Character or Plr.CharacterAdded:Wait()
	
	for _, v in ipairs(Char:GetDescendants()) do
		if v:IsA("BasePart") then
			timeModule:Track(v)
		end
	end
	
	local ClickAnim = Char:WaitForChild("Humanoid"):LoadAnimation(Tool.OnUse)
	ClickAnim:Play()
	task.wait(0.10)
	Tool.Shell.Tick:Play()
	
	task.wait(0.8)
	Tool.Shell.Tick:Stop()
end)

Tool.ReverseTimeEvent.OnServerEvent:Connect(function(Plr)
	local Char = Plr.Character or Plr.CharacterAdded:Wait()
	Char:WaitForChild("Humanoid"):UnequipTools()
	
	timeModule:Reverse()
	timeModule:SetSpeed(2)
end)

while true do
	timeModule:Update(0.1)
	task.wait(0.1)
end
1 Like
--//Tool
local Tool: Tool = script.Parent
local timeModule = require(Tool:WaitForChild("TimeModule"))
--//Character
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
--//Functionality

-- Because you already have a debounce i'll use yours instead of creating a new one.
local Debounce = false

-- .5 second cooldown
local Cooldown = .5

local HasSetPosition = false

local CCE: ColorCorrectionEffect = nil

-- Move IdleAnim up here so we can stop it after we unequip
local IdleAnim: AnimationTrack = nil

Tool.Equipped:Connect(function()
	Tool.Shell.EquipSound:Play()
	Character["Right Arm"]:WaitForChild("ToolGrip").Part1 = Tool.Base

	local EquipAnim = Humanoid:WaitForChild("Animator"):LoadAnimation(Tool.Equip)
	EquipAnim:Play()
	task.wait(0.15)

	IdleAnim = Humanoid:WaitForChild("Animator"):LoadAnimation(Tool.Idle)
	IdleAnim:Play()
end)

-- Moved outside of Tool.Equipped so it doesn't constantly create new connections
-- upon equipping
Tool.Unequipped:Connect(function()
	IdleAnim:Stop()
	Character["Right Arm"]:WaitForChild("ToolGrip").Part1 = nil
end)

Tool.Activated:Connect(function()
	-- Because both conditions require Debounce to be false,
	-- we could just have it return if debounce is true
	if Debounce then return end
	Debounce = true
	
	if HasSetPosition then
		Tool.SetPositionEvent:FireServer()
		HasSetPosition = true
	else
		local Camera = workspace.CurrentCamera
		local Lighting = game.Lighting
		
		-- initialize color correction effect
		CCE = Instance.new("ColorCorrectionEffect",Lighting)
		CCE.TintColor = Color3.fromRGB(89, 89, 89)
		CCE.Contrast = 30
		
		-- Setup Tweens
		
		-- Instead of creating new instances each time the tool gets activated, these could be
		-- outside of the tool activated event callback
		local TweenGoal = {
			Contrast = 0
		}
		local Tweeninfo = TweenInfo.new(
			1,
			Enum.EasingStyle.Sine,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		
		local CCETween = game:GetService("TweenService"):Create(CCE,Tweeninfo,TweenGoal)
		CCETween:Play()

		--Tweening Camera
		-- Same thing here, TweenGoal, Tweeninfo, and Tween could be outside of the tool
		-- activated event callback
		local TweenGoal = {
			FieldOfView = 10
		}
		local Tweeninfo = TweenInfo.new(
			1,
			Enum.EasingStyle.Sine,
			Enum.EasingDirection.Out,
			0,
			true,
			0
		)
		local CameraTween = game:GetService("TweenService"):Create(Camera,Tweeninfo,TweenGoal)
		CameraTween:Play()
		
		-- This should also invoke a remote function that returns if it occured correctly
		Tool.ReverseTimeEvent:FireServer()
		HasSetPosition = false
	end
	
	-- Add the cooldown below so it runs every time the tool
	-- gets activated
	task.wait(Cooldown)
	Debounce = false
end)
1 Like

So I replace this in the local script, correct?

1 Like

Yes

(charsssssssssssssssssssssss)

Doesn’t work sadly.

ewojewqijqweijeqw

1 Like

Errors? or it just doesn’t work

1 Like

Basically, what the item does is that when the item is first used, it will save the players position. The second time it’s used, it will revert to the players original position. What I am trying to achieve is that I want the item to go on cooldown once the player reaches their original position

1 Like

I see, try this out.

--//Tool
local Tool: Tool = script.Parent
local timeModule = require(Tool:WaitForChild("TimeModule"))
--//Character
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
--//Functionality

-- Because you already have a debounce i'll use yours instead of creating a new one.
local Debounce = false

-- .5 second cooldown
local Cooldown = .5

local HasSetPosition = false

local CCE: ColorCorrectionEffect = nil

-- Move IdleAnim up here so we can stop it after we unequip
local IdleAnim: AnimationTrack = nil

Tool.Equipped:Connect(function()
	Tool.Shell.EquipSound:Play()
	Character["Right Arm"]:WaitForChild("ToolGrip").Part1 = Tool.Base

	local EquipAnim = Humanoid:WaitForChild("Animator"):LoadAnimation(Tool.Equip)
	EquipAnim:Play()
	task.wait(0.15)

	IdleAnim = Humanoid:WaitForChild("Animator"):LoadAnimation(Tool.Idle)
	IdleAnim:Play()
end)

-- Moved outside of Tool.Equipped so it doesn't constantly create new connections
-- upon equipping
Tool.Unequipped:Connect(function()
	IdleAnim:Stop()
	Character["Right Arm"]:WaitForChild("ToolGrip").Part1 = nil
end)

Tool.Activated:Connect(function()
	-- Because both conditions require Debounce to be false,
	-- we could just have it return if debounce is true
	if Debounce then return end
	Debounce = HasSetPosition

	if not HasSetPosition then
		Tool.SetPositionEvent:FireServer()
		HasSetPosition = true
	else
		local Camera = workspace.CurrentCamera
		local Lighting = game.Lighting

		-- initialize color correction effect
		CCE = Instance.new("ColorCorrectionEffect",Lighting)
		CCE.TintColor = Color3.fromRGB(89, 89, 89)
		CCE.Contrast = 30

		-- Setup Tweens

		-- Instead of creating new instances each time the tool gets activated, these could be
		-- outside of the tool activated event callback
		local TweenGoal = {
			Contrast = 0
		}
		local Tweeninfo = TweenInfo.new(
			1,
			Enum.EasingStyle.Sine,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)

		local CCETween = game:GetService("TweenService"):Create(CCE,Tweeninfo,TweenGoal)
		CCETween:Play()

		--Tweening Camera
		-- Same thing here, TweenGoal, Tweeninfo, and Tween could be outside of the tool
		-- activated event callback
		local TweenGoal = {
			FieldOfView = 10
		}
		local Tweeninfo = TweenInfo.new(
			1,
			Enum.EasingStyle.Sine,
			Enum.EasingDirection.Out,
			0,
			true,
			0
		)
		local CameraTween = game:GetService("TweenService"):Create(Camera,Tweeninfo,TweenGoal)
		CameraTween:Play()

		-- This should also invoke a remote function that returns if it occured correctly
		Tool.ReverseTimeEvent:FireServer()
		HasSetPosition = false
		
		task.wait(Cooldown)
		Debounce = false
	end
end)
1 Like