Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Waiting for player to press any key?

Asked by
farizarps 132
6 years ago
Edited 6 years ago

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.

1module.Input = function()
2    frame.Visible = true
3    local key = UserInputService.InputEnded:Wait()
4    frame.Visible = false
5    print(key.KeyCode)
6    return key.KeyCode
7end

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

1module.Input = function()
2    frame.Visible = true
3    local key = nil
4    repeat key = UserInputService.InputEnded:Wait() until UserInputService.InputEnded:Wait() ~= Enum.KeyCode.Unknown
5    frame.Visible = false
6    print(key.KeyCode)
7    return key.KeyCode
8end

this actually waits for the keypress but still returns unknown Keycode

0
Unless I’m missing something here the second one isn’t actually assigning your key variable to the KeyCode. DinozCreates 1070 — 6y
0
i forgot to put that in the question. Updated farizarps 132 — 6y

1 answer

Log in to vote
1
Answered by
farizarps 132
6 years ago

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.

1module.Input = function()
2    frame.Visible = true
3    local key = nil
4    repeat key = UserInputService.InputEnded:Wait() until key.KeyCode ~= Enum.KeyCode.Unknown
5    frame.Visible = false
6    print(key)
7    return key.KeyCode
8en
0
@farizarps good explanation. oftenz 367 — 6y
Ad

Answer this question