I made a tool and if a player spams the Mouse 1 button, it activates the tool many, many times. Is there a way to disable the MouseButton1 button until the cooldown is done?
There’s Tool.Enabled
, which, when disabled, will stop Tool:Activate
from firing.
Use a Debounce variable to help prevent players from spamming.
Example:
local debounce = false -- this is for the cooldown
mouse.Button1Down:Connect(function()
if not debounce then -- if debounce is false (rest of code won't run if variable is true)
debounce = true
-- your code
task.wait() -- time for cooldown here
debounce = false -- stops cooldown
end
end)
Do you want it so that if the player clicks repetitively you want a cooldown or just 1 cooldown per click?
Repetitively. I made a little gun tool that shoots projectiles. If the player clicks too many times, it fires very, very fast. I tried the debounce thing, but it didn’t fix it.
Oh, ok. Here’s a solution: (this works on local script)
ALSO PUT YOUR PROJECTILE CODE INSIDE THE -- your code
--// Variables
local runService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local clicksUntilCooldown = 5 -- increasing this will make it easier to cooldown
local currentClick = 0 -- don't modify this.
local cooldown = 5 -- edit this for your needs
local debounce = false
--// Functions
local function click()
if debounce == false then
currentClick += 1
-- your code
print(currentClick)
end
end
local function everySecond()
if currentClick >= clicksUntilCooldown then
debounce = true
print("cooldown!!")
wait(cooldown)
debounce = false
end
currentClick = 0
end
mouse.Button1Down:Connect(click)
spawn(function()
while wait(0.1) do -- lowering this will make it harder to cooldown
everySecond() -- not really a second
end
end)
Edit:
clicksUntilCooldown
is the maximum amount of clicks the player can click to for the cooldown to run. (only under the wait(0.1)
edit the wait() code at the FUNCTION**. If you decrease it that makes it harder to start cooldown. Increase it and it does the opposite.