How to do a mobile button

How can i do a button for mobile, like only mobile users can see and use it

2 Likes

You add a ScreenGui to StarterGui. Add a Frame to ScreenGui. Add a TextButton to the Frame.

You make a local script that detects for touch devices.

local userInputService = game:GetService("UserInputService")
 
if userInputService.TouchEnabled then
	print("The user's device has a touchscreen!")
else
	print("The user's device does not have a touchscreen!")
end

Read more about it here:
https://developer.roblox.com/en-us/api-reference/property/UserInputService/TouchEnabled

4 Likes

You could use ContextActionService | Roblox Creator Documentation to achieve that.

1 Like

Dont do that. Use Context Action Service. Like this:

-- loacal script in starter player scripts
local CAS = game:GetService("ContextActionService")
local function Action()
	print("Button Clicked")
end
local Button = CAS:BindAction("MYCOOLACTION", Action, true, Enum.KeyCode.F) -- the true is weather or not it's on mobile. Key code is for PC players.

Mobile:

PC:

2 Likes

ContextActionService’s BindAction has a parameter that allows you to give an option to create a touch button for mobile devices.

local ContextActionService = game:GetService('ContentActionService')

local function doSomething() 

end

ContextActionService:BindAction(
  'Action', -- name of the action 
  doSomething, -- the function that will run when pressed
  true -- to create a button
)
1 Like

Is there any way that i dont need to use functions?

What would be the point of the button then, if it has no behaviour

1 Like

I mean, a custom gui Instead of the default mobile button

You need to. Thats for what it does. Try it out and it’ll work. Trust me

1 Like

Oh then @urmamimayo’s idea would suit what you wanted

1 Like

Okay, thanks anyways

This text will be blurred

If you want to you can do something like this:

--- local script in starterplayerscript
local userInputService = game:GetService("UserInputService")
local PlayerGui = game.Players.LocalPlayer.PlayerGui
if userInputService.TouchEnabled then
	PlayerGui.Gui.ThingHere.Visible = true
else
	PlayerGui.Gui.ThingHere.Visible = false
end

This seems like it would work best if you are trying to only have a GUI that shows up on mobile and not PC.

By the way, is there any way i can change the Icon and position of the default button?

You can. Do this:

-- loacal script in starter player scripts
local CAS = game:GetService("ContextActionService")
local function Action()
	print("Button Clicked")
end
local Button = CAS:BindAction("MYCOOLACTION", Action, true, Enum.KeyCode.F) -- the true is weather or not it's on mobile. Key code is for PC players.
CAS:SetPosition("MYCOOLACTION", UDim2.new(poshere))
CAS:SetImage("MYCOOLACTION", "IMAGEHERE")
1 Like

So the position (UDim2) works like a Normal Gui right?

It does. Since it’s a udim 2. Just that positioning with Context action service is kinda weird. So keep that in mind. Im pretty sure the origin is at the jump button(not sure)

The reason to make your own UI button instead of using CAS is because it is way easier to edit and position the UI button, you have the properties window for all of that + add gradients and overhead text.

For scripting, it’s about the same. You’re still assigning the same function to the button, so it doesn’t really make any different than connecting UIS and calling a function.

3 Likes