Tool cooldown isn't changing properly

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)

in this part you should not yield the script try this one:

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)
                 
				spawn(function() ---using spawn to not yield the script
                 wait(10)
				unstunPlayer(hit)
              end
			elseif not humanHit then
				debounceTime = 1.75
				particlesTime = 1
			end
			
		elseif not hit then
			debounceTime = 1.75
			particlesTime = 1
			return
		end
		
		wait(debounceTime) ---waiting
		touchdebounce = false
	end
end)

Hm, I tried it out and it didn’t seem to work. I don’t exactly know what spawn(function() end) does and what it did here.

Also, I had recorded a video showing what’s wrong. Hopefully it explains my problem better, apologies if it’s a bit long.

(Don’t mind the weird walking animation, the animations uploaded weird.)

this create new thread in a script which mean execute the code in another script
but its more recomended to use couroutine coroutine | Roblox Creator Documentation

1 Like
spawn(function()
  while wait() do end
end)
print('no loops can stop me')
1 Like

Oh, I understand now.

Still though, it didn’t really work here.