I’m trying to make a tool that changes it’s use cooldown time if whether or not you had hit another player with it.
Hitting a player changes the cooldown to 10 seconds.
Hitting something that isn’t a player changes the cooldown to 1.75 seconds.
Hitting nothing… doesn’t change anything?
Hitting nothing is supposed change the cooldown to 1.75 seconds, though during my testing it didn’t do anything.
The solution might be very simple, I’m just starting out as a scripter.
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
print("Did not hit player.")
debounceTime = 1.75
particlesTime = 1
end
else
print("Did not hit anything.")
debounceTime = 1.75
particlesTime = 1
return
end
wait(2)
touchdebounce = false
end
end)
Full 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
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
print("Did not hit player.")
debounceTime = 1.75
particlesTime = 1
end
else
print("Did not hit anything.")
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)