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.

local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local uis = game:GetService("UserInputService")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://182491423"
function OnKeyPressed(inputObject, gameProcessEvent)
    if inputObject.KeyCode == Enum.KeyCode.Q then
        humanoid:LoadAnimation(animation):Play()
    end
end
uis.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:

local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local cas = game:GetService("ContextActionService") --gets CAS
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://182491423"

function OnKeyPressed(actionName, inputState, inputObject) --handling function
    if inputState == Enum.UserInputState.Begin then --checks if input state is "begin" which is somewhat similar to UIS.InputBegan
        humanoid:LoadAnimation(animation):Play()
    end
end

cas: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