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

How would I add a timer to my animation tool script?

So I can swing my weapon and it does damage but there is no limit/timer on when you can swing. What I mean is you can swing with no cooldown. How would I add a cooldown?

script.Parent.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
		animation:Play()
	end)
end)

Also here is the script for damage if its ever needed for this.

script.Parent.blade.Touched:connect(function(p)
    if script.Parent.CanDamage.Value == true then
        script.Parent.CanDamage.Value = false
        p.Parent.Humanoid:TakeDamage(5)
    else
        script.Parent.CanDamage.Value = true
        p.Parent.Humanoid:TakeDamage(5)
    end
end)

1 answer

✓ Accepted answer
local cooldown = false 
script.Parent.Equipped:Connect(function(Mouse)
    Mouse.Button1Down:Connect(function()
		if not cooldown then
   			cooldown = true
       		local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
      		animation:Play()
       		wait(5) -- cooldown timer
      		cooldown = false
		end -- Add this
    end)
end)

Try This

5 comments

0

Ok so when I'm standing still I can still rapidly reset/attack with my sword but it only does damage once every 5 seconds. If I move while attacking it has no cooldown and does infinite damage. so essentially it didnt cooldown my swinging but it did to the damage but this is all negated while moving

0

I edited the code try now

0

For line 10 of your code the parenthesis is underlined red and im getting the error,21:40:47.286 - Players.MrMeeper9.Backpack.Dull Dagger.LocalScript:10: Expected identifier when parsing expression, got ')'

0

My apology, forgot to add the end for the if statement.

0

No need to apologize, thanks for the help!

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