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

How can i make it so the function only gets triggered once every .5 seconds even if spamclicking?

Asked by
Echtic 128
5 years ago

Here's the script:

wait()
local player = game.Players.LocalPlayer
local tool = script.Parent


tool.Equipped:Connect(function(Mouse)


    Mouse.Button1Down:Connect(function()


    local ev = workspace.Events.PunchE

    ev:FireServer()


    end)

end)

I need it to have a cooldown so if a player spamclicks the event will only get triggered after the cooldown is up.

1 answer

Log in to vote
1
Answered by
hellmatic 1523 Moderation Voter
5 years ago

Use the debounce method:

wait()
local player = game.Players.LocalPlayer
local tool = script.Parent

local CanClick = true -- debounce bool, this will determine if the function can fire (true) or not (false).

tool.Equipped:Connect(function(Mouse)


    Mouse.Button1Down:Connect(function()

    if CanClick then -- same thing as: 'if CanClick == true'
        CanClick = false -- set the debounce to false to prevent the function from firing
        local ev = workspace.Events.PunchE

        ev:FireServer()

        --COOLDOWN--
        wait(.5)
        CanClick = true
    end


    end)

end)
0
I am very thankful! Echtic 128 — 5y
Ad

Answer this question