UserInputService Mobile Issue

Hi guys. I was wondering how I could copy and paste this code, but make it work for mobile players. This code works for PC, but not for mobile. I guess the “return” key is not registered by Roblox unless you’re on PC. Anyone know how I can fix this?

Code:

local Players = game.Players or game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local Client = Players.LocalPlayer
local Number = script.Parent
local CurrentText = Number.Text
local Theme = game.Workspace:WaitForChild("Theme", math.huge)

UserInputService.InputBegan:Connect(function(iObject, GProcessed)
	if iObject.KeyCode == Enum.KeyCode.Return then
		if Number.Text ~= CurrentText and tonumber(Number.Text) then
			Theme.Volume = tonumber(Number.Text)
			if 10 < tonumber(Number.Text) then
				print("Knuckles: NOPE! XD")
				Number.Text = CurrentText
			end
			CurrentText = Number.Text
		end
	end
end)

Hey! Well since the Return KeyCode does not exist on any mobile or tablet, you must find another way to do this.

You can make a button that is only visible to users who do not have a keyboard attached and are using a touch screen that behaves the same way as a player pressing Return on their keyboard!

Here’s a rough example…

if not game.UserInputService.KeyboardEnabled and game.UserInputService.TouchEnabled then
	button.Visible = true

	button.MouseButton1Down:Connect(function()
		if Number.Text ~= CurrentText and tonumber(Number.Text) then
			Theme.Volume = tonumber(Number.Text)
			if 10 < tonumber(Number.Text) then
				print("Knuckles: NOPE! XD")
				Number.Text = CurrentText
			end
			CurrentText = Number.Text
		end
	end)
end

Keys like Return don’t really exist on mobile. You would need to use something like a ContextActionService button to achieve this:

local Players = game.Players or game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local ContextActionService = game:GetService("ContextActionService")
local Client = Players.LocalPlayer
local Number = script.Parent
local CurrentText = Number.Text
local Theme = game.Workspace:WaitForChild("Theme", math.huge)

local function ReturnPressed()
	if Number.Text ~= CurrentText and tonumber(Number.Text) then
		Theme.Volume = tonumber(Number.Text)
		if 10 < tonumber(Number.Text) then
			print("Knuckles: NOPE! XD")
			Number.Text = CurrentText
		end
		CurrentText = Number.Text
	end
end

UserInputService.InputBegan:Connect(function(iObject, GProcessed)
	if iObject.KeyCode == Enum.KeyCode.Return then
		ReturnPressed()
	end
end)

ContextActionService:BindAction("ReturnPressed", function(Action, State, Object)
	if State == Enum.UserInputState.Begin then
		ReturnPressed()
	end
end, true)

ContextActionService:SetTitle("ReturnPressed", "YOUR TITLE HERE")

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.