Wait Until A Certain Key Is Pressed?

I’m essentially creating a dialouge system, and I want the dialogue to progress when the player hits Enter on their keyboard. My issue right now is that you have to hit Enter twice before the dialouge progresses, and I can’t figure out why. Here’s the code for both the event that processes the key press, and the piece of code that reacts to it:

--Debounce variable
local continueDialouge = false

--Basic function to detect when the enter key is pressed
UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Return then
		continueDialouge = true
		
		wait()
		
		continueDialouge = false
	end
end)

--Waiting until the key is pressed
repeat wait() until continueDialouge

Can anybody explain to me why this is happening? Thanks.

1 Like

What does this even do??

That part’s to be sure that the other functions read the boolean value in time.

Well, you should use a boolvalue instead. Use :GetPropertyChangedSignal(“Value”), and not repeat wait() until continueDialouge

1 Like

Use UserInputService.InputBegan:Wait, it’ll return the passed arguments through the event and yield until InputBegan is fired.

-- waiting until a key is pressed
local input = UserInputService.InputBegan:Wait()

-- check if its the right key
if input.KeyCode == Enum.KeyCode.Return then
    -- do stuff
end

Or this incase a user accidentally presses the wrong key.

while true do
	local input = UserInputService.InputBegan:Wait()

	if input.KeyCode == Enum.KeyCode.Return then
		break
	end
end

-- do stuff
3 Likes

But how would I loop it back through (wait again) if they don’t click the Enter key?

1 Like

If you mean check again if they didn’t click the key, then the second example should work for that.