Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how do i add a cooldown to this old wepon script?

Asked by 3 years ago

i want to add a cooldown to this old wepon script i made, can someone help me do that?

local tool = script.Parent

local canDamage = false



local function onTouch(otherPart)



    local humanoid = otherPart.Parent:FindFirstChild("Humanoid")



    if not humanoid then 

        return 

    end



    if humanoid.Parent ~= tool.Parent and canDamage then 

        humanoid:TakeDamage(5)

    else

        return

    end



    canDamage = false

end



local function slash()

    local str = Instance.new("StringValue")

    str.Name = "e" --idk why i made it e, i just edited the script that i used for an old game

    str.Value = "Slash" 

    str.Parent = tool

    canDamage = true

end



tool.Activated:Connect(slash)

tool.Handle.Touched:Connect(onTouch)

1 answer

Log in to vote
1
Answered by
0hsa 193
3 years ago
Edited 3 years ago

didn't understand what you mean by cooldown, damage? clicking?

local tool = script.Parent

local canDamage = false

local Cooldown = false

local WaitTime: number = 0.3 --// anything

local function onTouch(otherPart)
    local humanoid = otherPart.Parent:FindFirstChild("Humanoid")

    if not humanoid then return end

    if humanoid.Parent ~= tool.Parent and canDamage then 
        humanoid:TakeDamage(5)
    else
        return
    end

    canDamage = false
end

local function slash()
    if Cooldown then return end
    Cooldown = true

    local str = Instance.new("StringValue")

    str.Name = "e" --idk why i made it e, i just edited the script that i used for an old game
    str.Value = "Slash"
    str.Parent = tool

    canDamage = true

    wait(WaitTime)

    Cooldown = false
end

tool.Activated:Connect(slash)
tool.Handle.Touched:Connect(onTouch)
Ad

Answer this question