How would I make a cooldown after a certain time passed?

started scripting about a week ago, and I am trying to make a cool down whenever 5 seconds pass while the player is pressing a key and the player has to wait again to press it again,
Heres the code block that i was trying to do and im not sure if im doing this right or not.

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local eKeyPressed = false
local cooldown = 0.5
UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		eKeyPressed = true
		
		while eKeyPressed == true do
			wait()
			print("player pressed E")
			 if wait(cooldown) then
				print("cooling")
			end
		end
		
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		eKeyPressed = false
		if wait(cooldown) then
				print("cooling")
			end
	end
end)

I think this here should help: Tutorial:Dealing with debounce | Roblox Wiki | Fandom

1 Like

You can use a debounce that is set to true or false to check if the player can complete the action once again after doing it once.

local debounce = false
local cooldownTime = 0.5

local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E and debounce == false then
		debounce = true
		print("Pressed E!")
		wait(cooldownTime)
		debounce = false
	end
end)

Sorry if my explanation was confusing

1 Like

After reading my post again, i realize i made a mistake.

[quote=“sunyoid, post:1, topic:621912”]
while the player is pressing a key and the player has to wait again to press it again
[/quote] wasn’t what i meant, i mean like when the player is pressing a key or holding down the key for a long time, the event will end whenever the wait function is called.(hopefully that cleared some confusion)

Keep us updated on whether it’s fixed or not. I just got member access and am trying to do as much as possible to help.
Edit: Try incorporating a ScreenGui, I’ve found it relatively easy to detect inputs.

1 Like

So something like this:

local timeToHoldThatKey = 5
local holdingTime = 0
local holdingKey = false

local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E and debounce == false then
		holdingKey = true
		
		repeat
			wait(1)
			holdingTime = holdingTime + 1
		until holdingKey == false
	end
end)

userInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E and debounce == false then
		holdingKey = false
		
		if holdingTime >= timeToHoldThatKey then
			print("they held the key for 5 or more seconds")
		end
	end
end)

I think that would work well depending on the ability. If it’s a press ability maybe the timer isn’t that great of an idea (of course it could be set to 0.1 or so but i think it would complicate everything), but it would work great for charged abilities, Doomfist’s Rocket Punch in Overwatch is a great example of what that could be used for.

Without the timer(or a tick use) there would be no way to check how much time has passed.

Sorry for making it confusing, I was referring to the timeToHoldThatKey system

I’m confused about what you think is wrong with the script

Oh, my mistake, I didn’t understand the script properly and had to re-read it. I understand now. My apologies.

1 Like

I’ve read the script and it worked whenever it is held down for 5 seconds, but after the print functions prints out “They held the key…” and you click the E key again it keeps going, but this will have to do.

I can redo it just explain in as much detail as possible what you want it to do.

OK I found an error in my script and wrote this one

local timeToHoldThatKey = 5
local cooldownTime = 2
local holdingTime = 0
local holdingKey = false
local cooldown = false

local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E and cooldown == false then
		holdingKey = true
		cooldown = true
		
		repeat
			wait(1)
			holdingTime = holdingTime + 1
		until holdingKey == false
	end
end)

userInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E and cooldown == true then
		holdingKey = false
		
		if holdingTime >= timeToHoldThatKey then
			print("they held the key for 5 or more seconds")
		end
		
		wait(cooldownTime)
		
		cooldown = false
	end
end)

This one has a cooldown feature

I thought that the timeToHoldThatKey meant that you had to hold down said key for 5 seconds, making it a “charge” ability, where you have to hold the button before it can actually be used. Something like building up energy for shooting a laser, you hold it for 5 seconds because timeToHoldThatKey = 5, then when those 5 seconds are up, you let go, and the laser is shot.

alright, whenever the player is holding down the key its going to have the HumanoidRootPart velocity move for like 5 seconds(already have a script for this) whenever the player is holding down the key, once the player stops holding down the key and they want to hold it down again, its going to have a cooldown to prevent spam. i hope i explained it well enough.

@sunyoid I think I solved that problem here

in the userInputService.Inputended function is the debounce == false suppose to be there?

Yes but if you might want to change it to this

userInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E and debounce == false then
		holdingKey = false
		
		if holdingTime >= timeToHoldThatKey then
			print("they held the key for 5 or more seconds")
		end
		
		wait(cooldownTime)
		
		cooldown = false
	end
end)

and add the local debounce = false variable too at the top of it right?