Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

1 comment

0

I am very thankful!

This discussion is preserved as it was. New answers, comments, and votes are closed.