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

How to print something everytime a player double taps a key within 2 seconds ?

Hi, I tried to make a script which prints something everytime you double tap a key within 2 seconds, if the player does not double tap within 2 seconds, I want to print "too slow" . I used UserInputService to detect the key press, and I am using a localscript. Here is my code

local uis = game:GetService("UserInputService")

  

local count = 0

  

uis.InputBegan:connect(function(input, gp)

if input.KeyCode == Enum.KeyCode.F and not gp then

count = count + 1

local started = tick()

if count <= 2 then

local difference = (tick() - started)

if difference <= 2 then

print("you pressed in 2 seconds congrats")

else

count = 0

print("too slow")

end

end

end

end)

thank you -onepunchactivated

2 comments

0

Where is the script located?

0

I swear to to my life I will find the answer I'm a maniac

1 answer

started is declared within the function.

This means that each time it's called, a new started local variable is declared.

This is not what you want.

Simply declare it outside of a function such that it is available as an upvalue (external local variable) in the function.

local UserInputService = game:GetService("UserInputService");
local lastPress = tick(); --// notice where this is declared
local count = 0;

UserInputService.InputBegan:Connect(function(input, gpe)
	if (gpe or tick() - lastPress > 2) then
		return; --// interacted with game engine (typed in a textbox, or clicked a gui button, or pressed too late
	end

	lastPress = tick();
	count = count + 1;
	--// do other stuff
end);

1 comment

0

Hi, sorry for the late reply, I tried your code it didn't work for me. I think I might've written something wrong. I put the lastPress tick() outside of the function and did the if statment for under 2 seconds yet when I press a key and wait like 10 seconds it still registers it as a touble tap, want it not to register as a double tap

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