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

How do I trigger a function when a player doubletaps the space bar?

I know how to trigger functions in a localscript based on keypresses, but I specifically need one for when the player not only presses the spacebar(which I don't know how to call anyway), but presses it twice. Can someone show me some example code, or something?

1 answer

✓ Accepted answer

Use the UserInputService to watch for input, and keep track of how many times the spacebar has been pressed in the last, let's say, 1/5 of a second. Once it's been pressed twice, do whatever you want. Here's an example.

local s = 0 -- Initialize a counter.
game:GetService('UserInputService').InputBegan:connect(function(input, processed)
	if input.KeyCode == Enum.KeyCode.Space and not processed then -- If the spacebar was pressed and it wasn't processed by the chatbar:
		s = s + 1 -- Increase the counter by 1.
		if s >= 2 then -- If this is the second press:
			s = 0 -- Reset the counter.
			--[[ Do whatever you want here. ]]
		else -- Otherwise, if this is not the second press:
			delay(0.2, function() -- After 0.2 seconds:
				if s < 2 then -- If we still don't have two spacebar presses:
					s = 0 -- Reset the counter.
				end
			end)
		end
	end
end)

1 comment

0

Eh

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