I want a tool’s cooldown to be set to 10 seconds if the player had hit another player with the tool. If they didn’t, then the cooldown is 1.75 seconds.
The cooldown is 1.75 seconds, but after the first time the player hits another player, the cooldown sets to 10 seconds and doesn’t change back even if the tool isn’t hitting players.
(I am using NumberValues to hold the cooldown time.)
Script
local tool = script.Parent
local toolActive = false
local debounce = false
local touchdebounce = false
local debounceTime = tool.debounceTime.Value
local particles = tool.End:FindFirstChild("electricityParticles")
local particlesTime = tool.particlesTime.Value
local anim = Instance.new("Animation")
anim.Parent = tool
anim.AnimationId = "rbxassetid://6857805039"
local function stunPlayer(target)
if target.Parent:IsA("Model") then
local targetChar = target.Parent
for i, v in pairs(targetChar:GetChildren()) do
if v:IsA("Part") then
v.Anchored = true
end
end
end
end
local function unstunPlayer(target)
if target.Parent:IsA("Model") then
local targetChar = target.Parent
for i, v in pairs(targetChar:GetChildren()) do
if v:IsA("Part") then
v.Anchored = false
end
end
end
end
-- The function which sets the cooldown
tool.Handle.Touched:Connect(function(hit)
if toolActive == true then
if touchdebounce == true then return end
touchdebounce = true
if hit then
local humanHit = hit.Parent:FindFirstChild("Humanoid")
if humanHit then
if humanHit.Parent == tool then return end
print("Hit player!")
debounceTime = 10
particlesTime = 5
humanHit:TakeDamage(50)
stunPlayer(hit)
wait(10)
unstunPlayer(hit)
elseif not humanHit then
debounceTime = 1.75
particlesTime = 1
end
elseif not hit then
debounceTime = 1.75
particlesTime = 1
return
end
wait(2)
touchdebounce = false
end
end)
tool.Activated:Connect(function()
local character = tool.Parent
local human = character.Humanoid
if debounce == true then return end
debounce = true
toolActive = true
local animTrack = human:LoadAnimation(anim)
animTrack:Play()
animTrack.KeyframeReached:Connect(function(keyframeName)
if keyframeName == "startElectricity" then
if particles then
particles.Enabled = true
end
end
end)
animTrack.Stopped:Connect(function()
wait(particlesTime)
if particles then
particles.Enabled = false
end
end)
wait(debounceTime)
debounce = false
toolActive = false
end)