How can I make a mobile button?

Hello Developers!
I recently decided to add mobile support to my games so more people can play them, I recently learned about ContextActionService and I know how to make a button, what I am having trouble with is: it won’t do anything and just sits there, it is also really small for some reason. If anyone could give me a push in the right direction, I would appreciate it a lot

Thank you in advance

Have you checked this?

As @JackscarIitt said, check out the developer wiki on it.

I’m not sure if you already checked this out, but if you didn’t, you also have to bind the button to a key and you can control the size and position on the button.

i have, but i don’t quite understand how to operate it. I know it must be in a local script but other than that, i don’t really know how to do much else.

Well, ContextActionService can be used to create cool custom actions by using multiple keybinds :thinking:

Let’s say I have this:

local CAS = game:GetService("ContextActionService")

local function handleAction()
    --We will do our interactive stuff here
end
 
ContextActionService:BindAction("Interact", handleAction, true, Enum.KeyCode.T, Enum.KeyCode.ButtonR1)

Our local function will be what we’d wanna interact with whenever it’s triggered by a Keybind in our BindAction parameter

Now, I’ll say the parameters for what BindAction does:

  • The first parameter is a custom function name to whatever we want, heck we can even name it to “Pie” and it’d still work

  • The second parameter is connecting the function that will detect its change when it’s called (Or handleAction

  • The third parameter is a Bool check if we want to make it a Mobile Button, we’d wanna set that to true

  • The last parameter are just a specific amount of keybinds we want to use to trigger the action when it’s called (Or the keys on your keyboard when pressing a special skill Z, X, C)

Next what we can do, is set our properties/preferences for our Mobile Button & what we wanna do with it

We’ll first start with the Title Name, we can just simply do:

ContextActionService:SetTitle("Interact", "Talk")

The first parameter Interact should be our custom action that we want to set our title to (That’ll be going for the rest of our properties changing), and our Title Name will be “Talk”

Next, we’ll change the Image ID:

ContextActionService:SetImage("Interact", "rbxassetid://0123456789")

The ID should be taken as an asset in the form of rbxassetid://00000000

Last, we’ll set the position:

ContextActionService:SetPosition("Interact", UDim2.new(1, -70, 0, 10))

UDim2 is a UI coordinate that’s mainly used for positioning/sizing UI Objects such as: Frames, TextButtons, ImageLabels, etc

And that should be it hopefully

13 Likes

While ContextActionService is a good service for mobile buttons, you can also add custom buttons by making a ScreenGUI and inserting a TextButton or ImageButton whatever you want. Then you would insert a localscript:

script.Parent.MouseButton1Click:Connect(function()
	-- Function
end)
1 Like