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

LocalScript does not debounce? What must I change?

Instead of doing a cooldown, it prints "Pressed e" as soon as I press it with no cooldown between presses, then all the "yes"s come in 5 seconds later.

local debounce = false
	 
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
	 
if debounce == false then
	  debounce = true --Now your code will not run, because debounce is not false.
	 
	   mouse.KeyDown:connect(function(key)
	       if key == "e" then
	         print("Pressed e")
	       wait(5)
			print("yes")
	       debounce = false --Now your code will run again.
	   end
	   end)
end


1 answer

✓ Accepted answer

You need to put the debounce inside the key down event, perhaps like this:

local debounce = false
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
mouse.KeyDown:connect(function(key)
	if not debounce and key == "e" then
		debounce = true
		print("Pressed e")
		wait(5)
		print("yes")
		debounce = false
	end
end)

1 comment

0

EDIT: combined line 5 and 6 to "if not debounce and key == "e" then" just because it looks better and is more efficient.

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