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.
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