How to make PC custom control script work on mobile?

So I have a script from a while ago, I forget where where I found it (it is not mine), and it is used to make the player control their character when their character is a ball. This gives them the ability to roll around as a ball. This script works perfectly fine on PC, but when I test it on mobile it doesn’t work because the script is set to detect when ‘WASD’ is pushed. Is there a way I can make the script detect if the player pushes ‘WASD’ or uses the mobile D-pad?

This the the part of the script that does this (thanks to Synical in the replies for the non-deprecated version)

local Controls = {LR = 0, UD = 0}
local UIS = game:GetService("UserInputService")

function KeyDown(input, gp)
	if gp then return end
	
	if input.KeyCode == Enum.KeyCode.W then
		Controls.UD=1
	elseif input.KeyCode == Enum.KeyCode.S then
		Controls.UD=-1
	elseif input.KeyCode == Enum.KeyCode.A then
		Controls.LR=-1
	elseif input.KeyCode == Enum.KeyCode.D then
		Controls.LR=1
	end
end

function KeyUp(input, gp)
	if gp then return end
	
	if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.S then
		Controls.UD=0
	elseif input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.D then
		Controls.LR=0
	end
end



--later in script when script detects that the player is using it
UIS.InputBegan:Connect(KeyDown)
UIS.InputEnded:Connect(KeyUp)

Thanks for any help!

I have also just heard about something with keydown being deprecated? Is this true?

Yes, it was deprecated long ago I believe. You should use ContextActionService or UserInputService to detect player input.

A simple fix to your script by updating it to UIS would be as simple as:

local Controls = {LR = 0, UD = 0}
local UIS = game:GetService("UserInputService")

function KeyDown(input, gp)
	if gp then return end
	
	if input.KeyCode == Enum.KeyCode.W then
		Controls.UD=1
	elseif input.KeyCode == Enum.KeyCode.S then
		Controls.UD=-1
	elseif input.KeyCode == Enum.KeyCode.A then
		Controls.LR=-1
	elseif input.KeyCode == Enum.KeyCode.D then
		Controls.LR=1
	end
end

function KeyUp(input, gp)
	if gp then return end
	
	if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.S then
		Controls.UD=0
	elseif input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.D then
		Controls.LR=0
	end
end

UIS.InputBegan:Connect(KeyDown)
UIS.InputEnded:Connect(KeyUp)

Or with CAS:

local Controls = {LR = 0, UD = 0}
local CAS = game:GetService("ContextActionService")
local mobileButton = true

local function W(actionName, inputState, input)
	if inputState ~= Enum.UserInputState.Begin then
		Controls.UD=1
	else
		Controls.UD=0
	end
end

local function S(actionName, inputState, input)
	if inputState ~= Enum.UserInputState.Begin then
		Controls.UD=-1
	else
		Controls.UD=0
	end
end

local function A(actionName, inputState, input)
	if inputState ~= Enum.UserInputState.Begin then
		Controls.LR=-1
	else
		Controls.LR=0
	end
end

local function D(actionName, inputState, input)
	if inputState ~= Enum.UserInputState.Begin then
		Controls.LR=1
	else
		Controls.LR=0
	end
end

CAS:BindAction("W_Control", W, mobileButton, Enum.KeyCode.W)
CAS:BindAction("S_Control", S, mobileButton, Enum.KeyCode.S)
CAS:BindAction("A_Control", A, mobileButton, Enum.KeyCode.A)
CAS:BindAction("D_Control", D, mobileButton, Enum.KeyCode.D)

I’m still not sure that CAS would be the way to go. You would have 4 onscreen buttons, one for each direction which is a bit annoying to use. I would try and look into joystick movement.

You could maybe do something similar with CAS by manipulating the onscreen buttons to look and form a DPAD although that might be really difficult.

3 Likes

Yeah the CAS version is a bit wonky but the UIS version works great! Although, I still just don’t know how I can make it work with the mobile DPAD/joystick.

Anyone know a method to fix this? I really need this to work