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

How do I make a cool down so you can only click on a gui every 0.5 seconds?

Asked by 1 year ago

So I'm trying to make it so when you click on a textbutton it adds 1. I made that here:

block = 1
game.ReplicatedStorage.anything.OnServerEvent:Connect(function(player)
    player.leaderstats.Blocks.Value = player.leaderstats.Blocks.Value + block
    wait(1)
end)

But, if you use an autoclicker, you can get hundreds of points in seconds. I was wondering how you could add a cooldown so you only get a point every 0.5 seconds? Thanks.

1 answer

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

Use a debounce.

-- LocalScript inside the button
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remote = ReplicatedStorage:WaitForChild("anything")
local button = script.Parent

local clickable = true -- this will be our debounce
button.MouseButton1Down:Connect(function()
    if clickable then -- if clickable is set to true
        clickable = false
        remote:FireServer()

        task.delay(0.5, function() -- this will turn clickable to true again after 0.5 seconds
            clickable = true
        end)
    end
end)

An alternative is the Active property.

-- LocalScript inside the button
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remote = ReplicatedStorage:WaitForChild("anything")
local button = script.Parent

button.MouseButton1Down:Connect(function()
    button.Active = false -- deactivates the button, making the button not clickable
    remote:FireServer()

    task.delay(0.5, function() -- this will make the button clickable again after 0.5 seconds
        button.Active = true
    end)
end)
Ad

Answer this question