Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I create mobile buttons?

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

✓ Accepted answer

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

1 comment

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.