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)