I need some help with adding mobile support into my 2d game script

So, i wanted to add mobile support in my game, however i dont know how mobile’s thumbstick even work.

What do i want to achieve with this? Making the mobile thumbstick be able to move the player either with some angles (similar to using a dpad or controlling it normally)

local UIS = game:GetService("UserInputService")
local runservice = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")
local ButtonToMove = script.Parent.Player
local MovementDistance = 0.01
local KeysDown = {}

local UIFolder = script.Parent.Attacks
local Keys = {
	Up = Enum.KeyCode.W;
	Left = Enum.KeyCode.A;
	Right = Enum.KeyCode.D;
	Down = Enum.KeyCode.S
}

script.Parent.Detect.Enabled = true
task.wait(0.05)

-- Boundaries
local function ClampPosition(position)
	-- Define boundaries
	local minX = 0.26
	local maxX = 0.692
	local minY = 0.394
	local maxY = 0.802

	-- Clamp
	local clampedX = math.clamp(position.X.Scale, minX, maxX)
	local clampedY = math.clamp(position.Y.Scale, minY, maxY)

	return UDim2.new(clampedX, position.X.Offset, clampedY, position.Y.Offset)
end

ButtonToMove.Position = ClampPosition(ButtonToMove.Position + UDim2.fromScale(0, -MovementDistance))

while script.Enabled == true and game.Players.LocalPlayer do
	if UIS.KeyboardEnabled then
		local downCount = 0
		UIS.InputBegan:Connect(function(Input, gameprocessed)
			task.wait(0.001)
			if gameprocessed then 
				return
			else
				local LocaleKeycode = Input.Delta

				if LocaleKeycode == Keys.Up or LocaleKeycode == Keys.Down or LocaleKeycode == Keys.Right or LocaleKeycode == Keys.Left then
					if KeysDown[LocaleKeycode] then return end 

					KeysDown[LocaleKeycode] = true
					KeysDown[LocaleKeycode].Delta.Y = Input.Delta.Y
					KeysDown[LocaleKeycode].Delta.X = Input.Delta.X
					downCount += 1

					if downCount > 1 then
						return
					else
						repeat
							local downCount = 0
							if KeysDown[LocaleKeycode].Delta.Y > 0 then
								ButtonToMove.Position = ClampPosition(ButtonToMove.Position + UDim2.fromScale(0, -MovementDistance))
							end
							if KeysDown[LocaleKeycode].Delta.Y < 0 then
								ButtonToMove.Position = ClampPosition(ButtonToMove.Position + UDim2.fromScale(0, MovementDistance))
							end
							if KeysDown[LocaleKeycode].Delta.X > 0 then					
								ButtonToMove.Position = ClampPosition(ButtonToMove.Position + UDim2.fromScale(MovementDistance - MovementDistance/1.5, 0))
							end
							if KeysDown[LocaleKeycode].Delta.X < 0 then
								ButtonToMove.Position = ClampPosition(ButtonToMove.Position + UDim2.fromScale(-MovementDistance + MovementDistance/1.5, 0))
							end
							task.wait(0.001)

						until downCount <= 0

					end      
				end
			end
		end)
		task.wait(0.001)
		UIS.InputEnded:Connect(function(Input, gameprocessed)
			if gameprocessed then 
				return
			else
				local LocaleKeycode = Input.Delta
				if LocaleKeycode.X and LocaleKeycode.Y then
					KeysDown[LocaleKeycode].Delta.Y = nil
					KeysDown[LocaleKeycode].Delta.X = nil
					KeysDown[LocaleKeycode] = nil
					downCount -= 1
				end
			end
		end)
	elseif UIS.KeyboardEnabled then
		local downCount = 0
		UIS.InputBegan:Connect(function(input, gameprocessed)
			task.wait(0.001)
			if gameprocessed then 
				return
			else
				local LocaleKeycode = input.KeyCode

				if LocaleKeycode == Keys.Up or LocaleKeycode == Keys.Down or LocaleKeycode == Keys.Right or LocaleKeycode == Keys.Left then
					if KeysDown[LocaleKeycode] then return end 

					KeysDown[LocaleKeycode] = true
					downCount += 1

					if downCount > 1 then
						return
					else
						repeat
							if KeysDown[Keys.Up] then
								ButtonToMove.Position = ClampPosition(ButtonToMove.Position + UDim2.fromScale(0, -MovementDistance))
							end
							if KeysDown[Keys.Down] then
								ButtonToMove.Position = ClampPosition(ButtonToMove.Position + UDim2.fromScale(0, MovementDistance))
							end
							if KeysDown[Keys.Right] then
								ButtonToMove.Position = ClampPosition(ButtonToMove.Position + UDim2.fromScale(MovementDistance - MovementDistance/1.5, 0))
							end
							if KeysDown[Keys.Left] then
								ButtonToMove.Position = ClampPosition(ButtonToMove.Position + UDim2.fromScale(-MovementDistance + MovementDistance/1.5, 0))
							end
							task.wait(0.001)
						until downCount <= 0 
					end      
				end
			end
		end)
		task.wait(0.001)
		UIS.InputEnded:Connect(function(input, gameprocessed)
			if gameprocessed then 
				return
			else
				local LocaleKeycode = input.KeyCode
				if LocaleKeycode == Keys.Up or LocaleKeycode == Keys.Down or LocaleKeycode == Keys.Right or LocaleKeycode == Keys.Left then
					KeysDown[LocaleKeycode] = nil
					downCount -= 1
				end
			end
		end)
	end
end

(My current movement script for pc, its kind of big so i only included some of the functions, ClampPosition() basically moves buttontomove up to a specific border.)

Video of it in action (pc)

1 Like