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

How would I make cooldowns after a punching animation with math.random animation punches?

So I was looking for a tutorial on how to make cooldowns after clicking. Like wait(1) after a punch but I was too specific and couldn't find any. Here's the local script, and tell me instructions.

Local script:

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")

tool.Equipped:connect(function()
	local idle = script.Parent.Parent.Humanoid:LoadAnimation(script.Idle)
	idle:Play()
end)

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

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

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

1 comment

0

CanAttack is a bool value

1 answer

If you add a debounce I guess it could help you out.

By using a debounce it waits for the function to finish in order to restart it.

local deb = false
local cooldown = 20

In the function

if deb == true then return end
deb = true

Here we're going to place these two in the beginning of our function. Basically here if the function has already been attempted to start again before the time ran out it will return it and cause it to not play.

wait(cooldown)
deb = false

Here is the cooldown you wanted, we already defined how long it's going to be up top now we can place it here so that this is how much time it takes for the function to actually be able to get up and going again.

Adding the debounce your script will come out as

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 deb = false
local cooldown = 20

tool.Equipped:connect(function()
    local idle = script.Parent.Parent.Humanoid:LoadAnimation(script.Idle)
    idle:Play()
end)

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

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

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

3 comments

0

I have explained it.

0

nope, doesn't work, when i click, I can't punch anymore.

0

its still the same problem when I change the cooldown.

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