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

How would i go about creating a cooldown for invisibility power?

Asked by 3 years ago

I just need help because whenever I try creating a cooldown, you can still spam the invisibility power.

game.Players.LocalPlayer:GetMouse().KeyDown:Connect(function(key)
    if key == "e" then
        local TS = game:GetService("TweenService")
        local Info = TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
        for _, v in pairs(script.Parent:GetDescendants()) do
            if v:IsA("BasePart") and v.Name~= "HumanoidRootPart" then
                TS:Create(v, Info, {Transparency=1}):Play()
            end
        end
        wait(20)
        local TS = game:GetService("TweenService")
        local Info = TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
        for _, v in pairs(script.Parent:GetDescendants()) do
            if v:IsA("BasePart") and v.Name~= "HumanoidRootPart" then
                TS:Create(v, Info, {Transparency=0}):Play()
            end
        end 
        --

    end
end)

1 answer

Log in to vote
1
Answered by
7777ert 49
3 years ago

Using debounce would be a good idea when making cooldowns:

local debounce = false

game.Players.LocalPlayer:GetMouse().KeyDown:Connect(function(key)
    if key == "e" then
        if not debounce then
            debounce = true
            local TS = game:GetService("TweenService")
            local Info = TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
            for _, v in pairs(script.Parent:GetDescendants()) do
                if v:IsA("BasePart") and v.Name~= "HumanoidRootPart" then
                    TS:Create(v, Info, {Transparency=1}):Play()
                end
            end
            wait(20)
            local TS = game:GetService("TweenService")
            local Info = TweenInfo.new(2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
            for _, v in pairs(script.Parent:GetDescendants()) do
                if v:IsA("BasePart") and v.Name~= "HumanoidRootPart" then
                    TS:Create(v, Info, {Transparency=0}):Play()
                end
            end 
            wait(5) -- change cooldown time
            debounce = false
        end
    end
end)

In this script, a variable debounce was added (you can change the variable name if you want to). It was false at first. When you pressed E, it will check if debounce is false. If yes, if will turn debounce to true and run the tween. Then it will wait until it finished cooling down and turn it to false again.

Ad

Answer this question