Damage debounce failing?

Hi! I have a script that’s working, but isn’t registering the debounce. Any chance you know how to help me with this?

  1. What do you want to achieve? Script registers debounce.

  2. What is the issue?
    robloxapp-20221225-1758136.wmv (6.4 MB)

  3. What solutions have you tried so far? Tried adding extra waiting time, nope.

Here’s the code for it.

local Tool = script.Parent
local Cooldown = Tool.Cooldown
local AtkPower = Tool["Attack Power"]

Cooling = false
Active = false
DamageDebounce = false

local Animation = Tool["Swing Animation1"]
local Animation2 = Tool["Swing Animation2"]
local Humanoid = nil
local Character = nil

Tool.Equipped:Connect(function()
	Tool.Handle.Transparency = 0
	Tool.Handle.Unsheath:Play()
end)

Tool.Activated:Connect(function()
	Active = true
	local Sword = Tool.Handle
	local AnimNumber = math.random(1, 2)
	if Tool:FindFirstAncestorOfClass("Model") then
		Character = Tool.Parent
		if Character:FindFirstChild("Humanoid") and Cooling == false and AnimNumber == 1 then
			Cooling = true
			Humanoid = Character.Humanoid
			local Track = Humanoid:LoadAnimation(Animation)
			Track.Priority = Enum.AnimationPriority.Action4
			Track:Play()
			Sword.Touched:Connect(function(hit)
				DamageDebounce = false
				local EnemyModel = nil
				local Enemy = nil
				if Active == true and hit.Parent.ClassName == "Model" and DamageDebounce == false then
					DamageDebounce = true
					EnemyModel = hit.Parent
					if EnemyModel:FindFirstChild("HumanoidE") then
						Enemy = EnemyModel.HumanoidE
						Enemy:TakeDamage(4)
					end
					task.wait(0.2)
					DamageDebounce = false
				end
			end)
			Track.Stopped:Connect(function()
				print("Animation1 ended.")
				Active = false
			end)
			task.wait(Cooldown.Value)
			Cooling = false
		elseif Character:FindFirstChild("Humanoid") and Cooling == false and AnimNumber == 2 then
			Cooling = true
			Humanoid = Character.Humanoid
			local Track = Humanoid:LoadAnimation(Animation2)
			Track.Priority = Enum.AnimationPriority.Action4
			Track:Play()
			Sword.Touched:Connect(function(hit)
				DamageDebounce = false
				local EnemyModel = nil
				local Enemy = nil
				if Active == true and hit.Parent.ClassName == "Model" and DamageDebounce == false then
					DamageDebounce = true
					EnemyModel = hit.Parent
					if EnemyModel:FindFirstChild("HumanoidE") then
						Enemy = EnemyModel.HumanoidE
						Enemy:TakeDamage(4)
					end
					task.wait(0.2)
					DamageDebounce = false
				end
			end)
			Track.Stopped:Connect(function()
				print("Animation2 ended.")
				Active = false
			end)
			task.wait(Cooldown.Value)
			Cooling = false
		end
	end
end)

Tool.Unequipped:Connect(function()
	Active = false
	DamageDebounce = false
	Tool.Handle.Transparency = 1
end)

Got any help or tips? Please send them.

1 Like

I’d recommend you use a timed debounce, not a boolean one. What I mean by that is, cache the time since the last activation (or the time until it can be active again), and when the event is fired check if it’s been long enough. This reduces the amount of threads that are running at a time, because there’s no need to wait() and reset the debounce. It also might help simplify your code.

If you want to take it a step further, you could even disconnect the event until damage is ready to be done again.

Btw the best way to cache time would probably be time()

Could you give me a code example? I’m a bit confused by your suggestion.

1 Like
local debounceTime = 3

local lastFired

event:Connect(function()
    if lastFired and time() < lastFired + debounceTime then return end

    doSomeStuff()

    lastFired = time()
end)

alternatively you could also disconnect the event

local debounceTime = 3
local event = event

local connection


local function connectEvent()
    connection = event:Connect(onEvent)
end

local function onEvent()
    connection:Disconnect()

    doSomeStuff()--if any code in this function yields it might change the behavior of the debounce

    task.delay(debounceTime, connectEvent)
end

connectEvent()

Never mind, I fixed it. The script didn’t work because I placed multiple debounces. Sorry for wasting your time.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.