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

How to add a cooldown to the animation code?

Hello (again)! I have attempted to implement a wait time into every line of the code shown down below, and nothing has managed to delay the animation. I'm sorry about taking up so much room on the question board, but I really want to get this animation over with so I can get back to modeling.

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local tool = script.Parent
local swingsound = tool:WaitForChild("Swing")
local canattack = tool:WaitForChild("CanAttack")
local Punch1 = Instance.new("Animation")
Punch1.AnimationId = "rbxassetid://4921775472"
local Punch2 = Instance.new("Animation")
Punch2.AnimationId = "rbxassetid://4921778051"
local cooldown = 1

tool.Activated:connect(function()
	local choose = math.random(1,2)
	canattack.Value = true
	if choose == 1 then
		local swing1animation = script.Parent.Parent.Humanoid:LoadAnimation(Punch1)
		swingsound:Play()
		swing1animation:Play()
	elseif choose == 2 then
		local swing2animation = script.Parent.Parent.Humanoid:LoadAnimation(Punch2)
		swingsound:Play()
		swing2animation:Play()
	end
end)

tool.Deactivated:connect(function()
	canattack.Value = true
end)

tool.Unequipped:connect(function()
	local hum = char:WaitForChild("Humanoid")
	for _,anim in pairs(hum:GetPlayingAnimationTracks()) do
 		anim:Stop()
	end
end)

Thanks again!

2 comments

0

Try to put a Delay at the line 15. Delay(time, function)

0

Just attempted integrating that, there still is no cooldown and I am able to spam the animation.

1 answer

fix (i think):

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:wait()
local tool = script.Parent
local swingsound = tool:WaitForChild("Swing")
local canattack = tool:WaitForChild("CanAttack")
local Punch1 = Instance.new("Animation")
Punch1.AnimationId = "rbxassetid://4921775472"
local Punch2 = Instance.new("Animation")
Punch2.AnimationId = "rbxassetid://4921778051"
local cooldown = 1

tool.Activated:connect(function()
    local choose = math.random(1,2)
    canattack.Value = true
    if choose == 1 then
	wait(cooldown)
        local swing1animation = script.Parent.Parent.Humanoid:LoadAnimation(Punch1)
        swingsound:Play()
        swing1animation:Play()
    elseif choose == 2 then
	wait(cooldown)
        local swing2animation = script.Parent.Parent.Humanoid:LoadAnimation(Punch2)
        swingsound:Play()
        swing2animation:Play()
    end
end)

tool.Deactivated:connect(function()
    canattack.Value = true
end)

tool.Unequipped:connect(function()
    local hum = char:WaitForChild("Humanoid")
    for _,anim in pairs(hum:GetPlayingAnimationTracks()) do
        anim:Stop()
    end
end)

1 comment

0

I tried this as well to no prevail.

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