I’m trying to make a script that yields until the player gives an input. I’ve already programmed for when the player is mobile, but I want the player to press a keyboard key instead of pressing the mouse. Can anyone help?
Here is the code I’ve come up with so far:
local InputType
if UserInputService.TouchEnabled then
InputType = Player:GetMouse().Button1Down
else
InputType = UserInputService.InputBegan
end
InputType:Wait()
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input)
repeat task.wait() until input.UserInputType == Enum.UserInputType.Keyboard
-- code here
end)
repeat
local inputObject, gameProcessed = userInputService.InputBegan:Wait()
until inputObject.KeyCode == Enum.KeyCode.K or inputObject.UserInputType == Enum.UserInputType.Touch
Here is a simple localscript that waits until a key on the keyboard is pressed:
local userInputService = game:GetService("UserInputService")
repeat
local input, gameProcessed = userInputService.InputBegan:Wait()
until input.UserInputType == Enum.UserInputType.Keyboard and not gameProcessed -- Game Processed is a variable that will be equal to true if the player is using the keyboard to type in the chat, or any other textbox. So if the player is typing in the chat, you might not want this event to succeed.
print("A player has pressed a key!")
Now, if you only want to stop yielding when the player has pressed a specific key, and just not any key in particular, then this block of code will yield until the player pressed ‘X’
local userInputService = game:GetService("UserInputService")
repeat
local input, gameProcessed = userInputService.InputBegan:Wait()
until not gameProcessed and input.KeyCode == Enum.KeyCode.X -- Game Processed is a variable that will be equal to true if the player is using the keyboard to type in the chat, or any other textbox. So if the player is typing in the chat, you might not want this event to succeed.
print("A player has pressed the 'X' key!")
You could try to detect what device the player is playing on, and then use the function that is designed for computers whenever the player is on computer, or use the function that is designed for mobile users whenever the player is on mobile.
Here are plenty of topics that talk about how to detect what device a player is using
I want to be able to use this, but is there a way that I can make a function that uses this if the player is on PC and another function that detects the players tap when the player is on mobile?
This is going to be a completely mobile compatible game, and when the player is on mobile it will utilize the mouse as the input (because then the player will just have to tap the screen to continue)
And in the code I’ve come up with that side of it works, but now I need to figure out how to utilize something like this so that when the player is on pc they have to press a keyboard key.
local InputType
if UserInputService.TouchEnabled then
InputType = Player:GetMouse().Button1Down
else
end
InputType:Wait()
But if I use some of the functions above instead of this, then only the computer side of it will work and mobile users will not be able to progress.
local Player = game:GetService("Players").LocalPlayer
local UIS = game:GetService("UserInputService")
local InputType
if UIS.TouchEnabled and not UIS.KeyboardEnabled then
InputType = Player:GetMouse().Button1Down
end
UIS.InputBegan:Connect(function(Input, gpe)
if Input.KeyCode == Enum.KeyCode.E then
--
end
end)
local function waitForInputType(inputType)
while true do
local input, processed = UserInputService.InputBegan:Wait()
if processed then continue end
if input.UserInputType == inputType then break end
end
end
if UserInputService.KeyboardEnabled then
waitForInputType(Enum.UserInputType.Keyboard)
else
waitForInputType(Enum.UserInputType.Touch)
end