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 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.
--//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
--//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)
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
--//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)