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 cooldown to this punch script?

It should be fairly ez lol.

CanDoDmg = false
Keybind = "l"
dmg = 10

local cooldown = false
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

mouse.KeyDown:Connect(function(key)
    if key == Keybind then
        if cooldown == false then
    cooldown = true
            CanDoDmg = true
            local anim = script.Parent.Humanoid:LoadAnimation(script.Animation)
            anim:Play()
            script.Parent.RightHand.Touched:connect(function(hit)
                if hit.Parent.Name == "Humanoid" then
                    if CanDoDmg == true then
                        hit.Parent.Humanoid:TakeDamage(dmg)
                        CanDoDmg = false
                        wait(1.5)
                        CanDoDmg = true
					end
                end
            end)
        end
    end
end)

1 answer

✓ Accepted answer

The other answers had a flaw, so I came up with this.

CanDoDmg = false
Keybind = "l"
dmg = 10

local cooldown = false
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

mouse.KeyDown:Connect(function(key)
    if key == Keybind then
        if cooldown == false then
    cooldown = true
            CanDoDmg = true
            local anim = script.Parent.Humanoid:LoadAnimation(script.Animation)
            anim:Play()
            script.Parent.RightHand.Touched:connect(function(hit)
                if hit.Parent.Name == "Humanoid" then
                    if CanDoDmg == true then
                        hit.Parent.Humanoid:TakeDamage(dmg)
                        CanDoDmg = false
                        wait(1.5)
                        CanDoDmg = true
                    end
                end
            end)
     wait(--//Wait Time Here\\--)
                 cooldown = false
        end
    end
end)

Should do the job. What the other answers did just reset the debounce if the humanoid was hit, but it would break the script if we didn't hit a humanoid.

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