Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would you add a debounce to a sword?

So i'm making a sword but it hurts people without me even swinging it. I added a thing that fixed that though but now you can spam the sword to as many times as you want making the debounce i made basically useless. Is there a way to make it so my sword doesn't hurt people without being activated and have it have a swing cool down?

Here is my script.

script.Parent.Equipped:Connect(function()
script.Parent.Activated:Connect(function()
script.Parent.Blade.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') then
		hit.Parent.Humanoid:TakeDamage(5)
		local clone = script:Clone()----------
		clone.Parent = script.Parent---------	 	Debounce
		script:Destroy()--------------
		end
	end)
	local animation2 = script.Parent.SideSwipe
		local play2 = script.Parent.Parent:WaitForChild("Humanoid"):LoadAnimation(animation2)
		play2:Play()
		script.Parent.Attack:Play()
	end)
end)

1 answer

✓ Accepted answer

You can add the debounce so

local Debounce = false

Set Debounce to true when the tool activates and set it to false after a second

script.Parent.Activated:Connect(function()
       Debounce = true
       -- Plays the swing animation
       AnimationTrack.Stopped:Connect(function() -- AnimationTrack is when u do the :LoadAnimtion() Function
              Debounce = false
       end)
end)

Let’s add the if statement to the Touched Event

script.Parent.Blade.Touched:Connect(function(hit)
       if Debounce then return end
       -- Humanoid Check and etc
end)

1 comment

0

Thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.