How to get mobile thumbstick input with ContextActionService?

I’m trying to make a pirate ship. You can already drive it but only on PC, now I’ll need to make mobile controls for it. But DevHub doesn’t seem to have any information about mobile thumbsticks with ContextActionService. Any information on how I can do that?

My code:

--!strict

local ContextActionService = game:GetService("ContextActionService")
local Players = game:GetService("Players")

local LocalPlayer = Players.LocalPlayer
local character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local moveForwardsEvent = script.MoveForwards.Value
local moveBackwardsEvent = script.MoveBackwards.Value
local turnRightEvent = script.TurnRight.Value
local turnLeftEvent = script.TurnLeft.Value
local bindActionsEvent = script.BindActions.Value
local unbindActionsEvent = script.UnbindActions.Value

local ACTION_JUMP = "OnPressJump"
local ACTION_FORWARD = "OnPressForward"
local ACTION_BACKWARD = "OnPressBackward"
local ACTION_RIGHT = "OnPressRight"
local ACTION_LEFT = "OnPressLeft"

function handleAction(actionName, inputState, inputObject)
	if actionName == ACTION_JUMP then
		if inputState == Enum.UserInputState.Begin then
			unbindActions()
			unbindActionsEvent:FireServer()
			script:Destroy()
		end
	elseif actionName == ACTION_FORWARD then
		if inputState == Enum.UserInputState.Begin then
			moveForwardsEvent:FireServer(true)
		elseif inputState == Enum.UserInputState.End or inputState == Enum.UserInputState.Cancel then
			moveForwardsEvent:FireServer(false)
		end
	elseif actionName == ACTION_BACKWARD then
		if inputState == Enum.UserInputState.Begin then
			moveBackwardsEvent:FireServer(true)
		elseif inputState == Enum.UserInputState.End or inputState == Enum.UserInputState.Cancel then
			moveBackwardsEvent:FireServer(false)
		end
	elseif actionName == ACTION_RIGHT then
		if inputState == Enum.UserInputState.Begin then
			turnRightEvent:FireServer(true)
		elseif inputState == Enum.UserInputState.End or inputState == Enum.UserInputState.Cancel then
			turnRightEvent:FireServer(false)
		end
	elseif actionName == ACTION_LEFT then
		if inputState == Enum.UserInputState.Begin then
			turnLeftEvent:FireServer(true)
		elseif inputState == Enum.UserInputState.End or inputState == Enum.UserInputState.Cancel then
			turnLeftEvent:FireServer(false)
		end
	end
end

function bindActions()
	ContextActionService:BindActionAtPriority(ACTION_JUMP, handleAction, false, 100, Enum.KeyCode.Space)
	ContextActionService:BindActionAtPriority(ACTION_FORWARD, handleAction, false, 100, Enum.KeyCode.W)
	ContextActionService:BindActionAtPriority(ACTION_BACKWARD, handleAction, false, 100, Enum.KeyCode.S)
	ContextActionService:BindActionAtPriority(ACTION_RIGHT, handleAction, false, 100, Enum.KeyCode.D)
	ContextActionService:BindActionAtPriority(ACTION_LEFT, handleAction, false, 100, Enum.KeyCode.A)
end

function unbindActions()
	ContextActionService:UnbindAction(ACTION_JUMP)
	ContextActionService:UnbindAction(ACTION_FORWARD)
	ContextActionService:UnbindAction(ACTION_BACKWARD)
	ContextActionService:UnbindAction(ACTION_RIGHT)
	ContextActionService:UnbindAction(ACTION_LEFT)
end

humanoid.Died:Connect(function()
	unbindActions()
	unbindActionsEvent:FireServer()
	script:Destroy()
end)

bindActions()
unbindActionsEvent.OnClientEvent:Connect(unbindActions)

1 Like

Are you looking for the Enum for mobile movement?
if so then Enum.UserInputType.Touch might be what youre looking for

Is there Enum.Keycode.SOMETHING for the thumbstick? So that I can bind it to handleAction.

Also, I’m not using UserInputService.

Oh my bad, in that case then no there isn’t a keycode for the thumbstick, only one for a controllers thumbstick, you can find the keycodes here though

and I found this as well about finding mobile movement (not sure if it’ll be of help to you)

1 Like

Have you found a solution to that? I’ve ran into the exact same issue now

Although it would not really allow contextactionservice to detect stuff, there are two things you can do.

For one, you could disable the thumbstick and create your own mobile buttons.

Other option might be using something around these lines:

local Direction = Character.HumanoidRootPart.CFrame:VectorToObjectSpace(Character.Humanoid.MoveDirection)
local Horizontal = math.round(Direction.X)
local Vertical = -math.round(Direction.Z)

although, it might not be suited for your needs.

I’ve attempted to use MoveDirection previously, it did not really help for the specific case, but that doesn’t put the touch input under the same roof with the PC inputs , in the context action service…
Creating my own thumbstick is even more tedious than just doing a workaround and ignoring context action service…