How to make a cooldown time?

Hello! I would like to know can I implement a cooldowntime in my game for my code:

-- Concentrate attack anim with the right mouse click:
	UserInputService.InputBegan:Connect(function(input, gameProccesedEvent)
		if input.UserInputType == Enum.UserInputType.MouseButton2 then
			print("Heavy attack activated")
			local playAnim = humanoid:LoadAnimation(con_hit_anim)
			playAnim:Play()
			script.BigHitAnim:FireServer()
		end
	end)
end) -- End of tool touched	

I would really appreciate if you could help me implement a cooldown time so that when pressing this button I have to wait 5 seconds to be able to press it again.

2 Likes
local Debounce = false
UserInputService.InputBegan:Connect(function(input, gameProccesedEvent)
	if not Debounce and input.UserInputType == Enum.UserInputType.MouseButton2 then
		Debounce = true
		print("Heavy attack activated")
		local playAnim = humanoid:LoadAnimation(con_hit_anim)
		playAnim:Play()
		script.BigHitAnim:FireServer()
		wait(5)
		Debounce = false
	end
end)

PS you should also add checks on the server to prevent exploiters from spamming the BigHitAnim event

1 Like

Don’t you know about wait()
Or would you like to use a more complex method

it didn’t worked for me :frowning:

-- Concentrate attack anim with the right mouse click:
	UserInputService.InputBegan:Connect(function(input, gameProccesedEvent)
		if input.UserInputType == Enum.UserInputType.MouseButton2 then
			print("Heavy attack activated")
			local playAnim = humanoid:LoadAnimation(con_hit_anim)
			playAnim:Play()
			script.BigHitAnim:FireServer()
		end
	end)
end) -- End of tool touched	

What’s with the second end

2 Likes

also with did u add a gameProcessedevent parameter when you did not eventually make use of it

2 Likes

**THANKS YOU!!! **
Thank you very much you saved the project for me

1 Like
--this is more reliable

local Debounce = false
UserInputService.InputBegan:Connect(function(input, gameProccesedEvent)
    if gameProccesedEvent then return end
	if not Debounce and input.UserInputType == Enum.UserInputType.MouseButton2 then
		Debounce = true
		print("Heavy attack activated")
		local playAnim = humanoid:LoadAnimation(con_hit_anim)
		playAnim:Play()
		script.BigHitAnim:FireServer()
		wait(5)
		Debounce = false
	end
end)

gameProccesedEvent is being made use of so when the player is typing in chat it will not run
rather than using other options like ContextActionService