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

How to add a cool down on my "Dash Script"?

Asked by 6 years ago

I am making a dash script and successfully did it but the players spams it. i want to fix this but i don't know how to do it.

The Code:

local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid````
local humanoid = player.Character.Humanoid
local mouse = player:GetMouse()

local anim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/asset/?id=1824890937" 

mouse.KeyDown:connect(function(key)
    if string.byte(key) == 50 then
        local playAnim = humanoid:LoadAnimation(anim)
        playAnim:Play()
        script.Sound:Play()
    player.Character.HumanoidRootPart.Velocity = player.Character.HumanoidRootPart.CFrame.lookVector * 125
        wait(1.5)
    end
    wait(10)
end)

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Assign the current machine time tick() to an upvalue (variable in an outer scope), subtract tick() from that variable and check if it's larger than an arbitrary number.

Example

local lastClicked, cooldown = 0, 2


UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if tick() - lastClicked >= cooldown then
        lastClicked = tick()
        -- ...
    end
end)
0
Where do i put it tho? im a newbie at scripting sorry KingCrebz 3 — 6y
0
on your if statement in line 50 you can make the condition 'string.byte(key) == 50 and tick() - lastClicked > cooldown' or something similar. LifeInDevelopment 364 — 6y
Ad

Answer this question