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

Waiting for player to press any key?

I'm working on a key-bind mapper and I have a function that's supposed to wait until the player presses any key and returns the key the player pressed.

module.Input = function()
	frame.Visible = true
	local key = UserInputService.InputEnded:Wait() 
	frame.Visible = false
	print(key.KeyCode)
	return key.KeyCode
end

this does not wait for keypress and instantly returns the Unknown Keycode. so i modified it:

module.Input = function()
	frame.Visible = true
	local key = nil
	repeat key = UserInputService.InputEnded:Wait() until UserInputService.InputEnded:Wait() ~= Enum.KeyCode.Unknown
	frame.Visible = false
	print(key.KeyCode)
	return key.KeyCode
end

this actually waits for the keypress but still returns unknown Keycode

2 comments

0

Unless I’m missing something here the second one isn’t actually assigning your key variable to the KeyCode.

0

i forgot to put that in the question. Updated

1 answer

I've fixed my problem. ill share so others can learn from it.

UserInputService.InputEnded:Wait() returns the inputObject, in order to know what key was presed, we use inputObject.KeyCode

In repeat key = UserInputService.InputEnded:Wait() until UserInputService.InputEnded:Wait() ~= Enum.KeyCode.Unknown because i was comparing an inputObject to a keycode it would return true.

module.Input = function()
	frame.Visible = true
	local key = nil
	repeat key = UserInputService.InputEnded:Wait() until key.KeyCode ~= Enum.KeyCode.Unknown
	frame.Visible = false
	print(key)
	return key.KeyCode
en

1 comment

0

@farizarps good explanation.

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