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 my ability?

Asked by 1 year ago

This ability is based on the keybind R in case you need to know that. This ability can continuously be spammed upon clicking R instead of waiting, lets say about 3 seconds, before being able to be used again. Is there a way I can add a cooldown? my code:

local tweenService = game:GetService("TweenService")

game.ReplicatedStorage.AirMagic.WindPushRE.OnServerEvent:Connect(function(player)
    local character = player.Character
    local properties = {
        Size = Vector3.new(60 ,60, 60)
    }
    local newForceField = game.ServerScriptService["Wind Magic"].WindPush.WPush:Clone()
    newForceField.Parent = game.Workspace
    newForceField.CFrame = character.HumanoidRootPart.CFrame

    local info = TweenInfo.new(0.9, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)

    local defaultWalkSpeed = character.Humanoid.WalkSpeed
    local defaultJumpPower = character.Humanoid.JumpPower
    character.Humanoid.WalkSpeed = 0
    character.Humanoid.JumpPower = 0
    character.Humanoid.MaxHealth = 100
    character.Humanoid.Health = 100

    local outTween = tweenService:Create(newForceField, info, properties)
    outTween:Play()

    wait(1)

    local properties2 = {
        Transparency = 0.75;
    }

    wait(1)

    local properties2 = {
        Transparency = 0.8;
    }

    wait(1)

    local properties2 = {
        Transparency = 0.9;
    }

    local info2 = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.In)
    local fadeTween = tweenService:Create(newForceField, info2, properties2)
    fadeTween:Play()
    wait(3)
    character.Humanoid.WalkSpeed = 16
    character.Humanoid.JumpPower = 50
    character.Humanoid.MaxHealth = 100
    character.Humanoid.Health = 100
    newForceField:Destroy()
end)

1 answer

Log in to vote
0
Answered by 1 year ago

I think your looking for Debounce, Debounce Is the cooldown

Example Script:

local Part = script.Parent
local Debounce = false

Part.Touched:Connect(function(hit)
    if not Debounce then
        local Humanoid = hit.Parent:WaitForChild("Humanoid")
        if Humanoid then
            Debounce = true
            print("Hey! Stop stepping on me! >:(")
            wait(1)
            Debounce = false
        end
    end
end)
0
thank you! I got confused since I managed to make a cooldown system without using Debounce and when I tried to add it to the current code it did not work but it does work on other code. I might have set it up wrong there. at least I can do it the proper way now too. ThatShadowRein 18 — 1y
0
No problem, always happy to help imnotaguest1121 362 — 1y
Ad

Answer this question