How to create mobile/tablet only GUI?

I want to create a GUI for mobile/tablet player for my game, it would be to execute some already existing script, but they don’t work for mobile because it using F (for a fflashlight) and SHIFT (to run), if anyone have any idea on how to execute it i would be happy if someone help me.

2 Likes

You could use the UserInputService to check the player’s device.

local uis = game:GetService("UserInputService")

task.wait(1)
local device = "Computer"
if uis.TouchEnabled and not uis.KeyboardEnabled then
    device = "Mobile"
end

--do what you will with this variable.

Or, if you bind the actions with ContextActionService, you can have it create a button for mobile players.

local cas = game:GetService("ContextActionService")
cas:BindAction("Something", functionNameHere, true --[[create button]], Enum.KeyCode.F)
3 Likes

i don’t understand what i need to do with those scipt

1 Like

Bro are you even know how to script? Or you’re making a Toolbox game which is not unique? (But added models)

Guy above explained you how to check player’s device. Script everything you need with that guy gave you! You’re just “Oh this is going to be easy todo. Games on Roblox looks easy to make”.

Just follow his code and you will understand! I’ll hope this will fix your problem and you won’t ask stupid questions don’t knowing how to script in damn Roblox Studio. Have a nice day!

jk

Try looking into ContextActionService. I’m pretty sure this is what you’re looking for.

Note that it’s not recommended to rely on TouchEnabled as a way of verifying whether or not a user is on mobile. There have been instances where KeyboardEnabled has erroneously shown as being enabled for mobile users, which could result in that conditional statement to fail.


@Natsugamerwolf on the Roblox Creator Documentation site, there is a guide for Cross-Platform Design, and the last section is for adjusting by input type. It provides an example of how you can detect the most recent input type; when a user touches the screen on a touch-screen enabled device, it would be Enum.UserInputType.Touch.

When that is the most recent InputType, you could enable an on-screen GUI that allows players to press a GuiButton to sprint (and when it is no longer the last input type, you can turn off the GUI, just in case the user had touch-screen enabled for a laptop, for example).

Here’s an example implementation based on the examples provided on that Roblox Creator Documentation page:

Example

-- Example ModuleScript from the Roblox Creator Documentation site
-- Code for a ModuleScript in the ReplicatedStorage, named "PlayerInput"

local UserInputService = game:GetService("UserInputService")

local PlayerInput = {}

PlayerInput.InputTypes = {
	KEYBOARD_MOUSE = "keyboard/mouse",
	TOUCH = "touch",
	GAMEPAD = "gamepad"
}

PlayerInput.CurrentInput = nil
PlayerInput.InputTypeChanged = Instance.new("BindableEvent")

local InputTypes = {
	[Enum.UserInputType.None] = nil,
	[Enum.UserInputType.TextInput] = nil,
	[Enum.UserInputType.InputMethod] = nil,

	[Enum.UserInputType.MouseButton1] = PlayerInput.InputTypes.KEYBOARD_MOUSE,
	[Enum.UserInputType.MouseButton2] = PlayerInput.InputTypes.KEYBOARD_MOUSE,
	[Enum.UserInputType.MouseButton3] = PlayerInput.InputTypes.KEYBOARD_MOUSE,
	[Enum.UserInputType.MouseWheel] = PlayerInput.InputTypes.KEYBOARD_MOUSE,
	[Enum.UserInputType.MouseMovement] = PlayerInput.InputTypes.KEYBOARD_MOUSE,
	[Enum.UserInputType.Keyboard] = PlayerInput.InputTypes.KEYBOARD_MOUSE,

	[Enum.UserInputType.Touch] = PlayerInput.InputTypes.TOUCH,
	[Enum.UserInputType.Accelerometer] = PlayerInput.InputTypes.TOUCH,
	[Enum.UserInputType.Gyro] = PlayerInput.InputTypes.TOUCH,

	[Enum.UserInputType.Gamepad1] = PlayerInput.InputTypes.GAMEPAD,
	[Enum.UserInputType.Gamepad2] = PlayerInput.InputTypes.GAMEPAD,
	[Enum.UserInputType.Gamepad3] = PlayerInput.InputTypes.GAMEPAD,
	[Enum.UserInputType.Gamepad4] = PlayerInput.InputTypes.GAMEPAD,
	[Enum.UserInputType.Gamepad5] = PlayerInput.InputTypes.GAMEPAD,
	[Enum.UserInputType.Gamepad6] = PlayerInput.InputTypes.GAMEPAD,
	[Enum.UserInputType.Gamepad7] = PlayerInput.InputTypes.GAMEPAD,
	[Enum.UserInputType.Gamepad8] = PlayerInput.InputTypes.GAMEPAD,
}

local function setPlayerInputType(userInputType)
	local playerInputType = InputTypes[userInputType]

	if playerInputType and playerInputType ~= PlayerInput.CurrentInput then
		PlayerInput.CurrentInput = playerInputType
		PlayerInput.InputTypeChanged:Fire(playerInputType)
	end
end

-- Initially set the player's input type
setPlayerInputType(UserInputService:GetLastInputType())

-- Update current input type based on last input type received
UserInputService.LastInputTypeChanged:Connect(setPlayerInputType)

return PlayerInput

And then here’s a modified version of the LocalScript code from that same guide, which matches your use case:

-- (Revised) Example LocalScript from the Roblox Creator Documentation site
-- Code for a LocalScript in the StarterPlayerScripts container

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayerInput = require(ReplicatedStorage:WaitForChild("PlayerInput"))

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local PlayerGui = player:WaitForChild("PlayerGui")

local sprintScreenGui = PlayerGui:WaitForChild("NameOfSprintGuiGoesHere")


local function activateScreenGui(newValue)
    if newValue == true then
        sprintScreenGui.Enabled = true

    else
        sprintScreenGui.Enabled = false
    end
end


-- Check the player's input type at any time by reading PlayerInput.CurrentInput
print("Player is using", PlayerInput.CurrentInput)

if PlayerInput.CurrentInput == "touch" then
    activateScreenGui(true)
end

-- Listen for input type changes with PlayerInput.InputTypeChanged event
PlayerInput.InputTypeChanged.Event:Connect(function(newInputType)
    if newInputType == "touch" then
        activateScreenGui(true)
    else
        activateScreenGui(false)
    end

	print("Input changed to", newInputType)
end)


Edit:

Alternatively, if that’s the only thing in your game that isn’t already mobile-compatible, a much simpler implementation that only needs a single LocalScript, could be the following:

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

local player = Players.LocalPlayer
local PlayerGui = player:WaitForChild("PlayerGui")
local sprintScreenGui = PlayerGui:WaitForChild("NameOfSprintGuiGoesHere")

---

local inputTypes = {
	Enum.UserInputType.Touch,
	Enum.UserInputType.Accelerometer,
	Enum.UserInputType.Gyro
}

local function activateScreenGui(newValue)
    if table.find(inputTypes, newValue) then
        sprintScreenGui.Enabled = true
    else
        sprintScreenGui.Enabled = false
    end
end

UserInputService.LastInputTypeChanged:Connect(activateScreenGui)
2 Likes

Relax everyone has started so. don`t be offesiv. If you mess up with starters then you have a problem with me. Cause RBS is the ultimatly beginner language(Lua). Nearly everyone whos scripting with it, has no idear what oop is. And manly how to use it.

2 Likes

This would just be a LocalScript anywhere such a script would run. You should give @StrongBigeMan9 's method a go, it’s better.

Or, above all the rest, just use ContextActionService - it would make it SO much easier. It would be much more efficient than the others.




(he did say it was a joke)

(Object-Oriented Programming isn’t a great example because it’s a big step from other programming)

@Fan_Len4ika1Tvink123 you were a bit harsh tho even if it was a joke

5 Likes

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