Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I create mobile buttons?

Asked by
ElBamino 153
3 years ago

https://developer.roblox.com/en-us/articles/ContextActionService-Creating-Mobile-Buttons The link above is what I'm trying to understand. So this link doesn't really help me much and, I was wondering if I could get some help on this? I have a sample code provided below on what I would like to turn into a mobile button, a button only available on mobile or tablet that does the same thing as key pressed on PC. Is this something I add onto this or, a whole new LocalScript?

The code below is a LocalScript in StarterPack.

01local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
02local character = player.Character or player.CharacterAdded:Wait()
03local humanoid = character:WaitForChild("Humanoid")
04local uis = game:GetService("UserInputService")
05local animation = Instance.new("Animation")
06animation.AnimationId = "rbxassetid://182491423"
07function OnKeyPressed(inputObject, gameProcessEvent)
08    if inputObject.KeyCode == Enum.KeyCode.Q then
09        humanoid:LoadAnimation(animation):Play()
10    end
11end
12uis.InputBegan:Connect(OnKeyPressed)

Thanks for the help in advance. I tried typing a test/sample code but, it didn't work because I don't how to organize and merge them together at this moment in time. I appreciate it!

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

Use ContextActionService! Here's how:

01local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
02local character = player.Character or player.CharacterAdded:Wait()
03local humanoid = character:WaitForChild("Humanoid")
04local cas = game:GetService("ContextActionService") --gets CAS
05local animation = Instance.new("Animation")
06animation.AnimationId = "rbxassetid://182491423"
07 
08function OnKeyPressed(actionName, inputState, inputObject) --handling function
09    if inputState == Enum.UserInputState.Begin then --checks if input state is "begin" which is somewhat similar to UIS.InputBegan
10        humanoid:LoadAnimation(animation):Play()
11    end
12end
13 
14cas:BindAction("PlayAnim", OnKeyPressed, true, Enum.KeyCode.Q) --binds the function to Q

There are certain functions of CAS that allow us to customize the appearance of the mobile button such as SetPosition, SetTitle, SetImage. Please check the API linked below for all info, hope this helps!

ContextActionService

BindAction

Creating Mobile Buttons

0
Oh shoot, thank you very much! I'm going to do some testing and more reading so I can learn more about it. Thanks for the links and help! I appreciate it. ElBamino 153 — 3y
Ad

Answer this question