Help with Mobile UserInputService

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    im trying to make a movement script for mobile basically player touches a part of the screen and the player moves
  2. What is the issue? Include screenshots / videos if possible!
    im not having any errors i juat wanna know if i can use TouchStarted and TouchEnded but it only happens when i touch on certain part of the screen

    here shows where i want to touch to make player move
    and here is the explorer view
    image
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    yes but no hope :frowning:
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local UserInputService = game:GetService("UserInputService")



local player = game.Players.LocalPlayer

local function touchStarted(input, gameProcessed)
	player:Move(Vector3.new(1,0,0))
end
 

local function touchEnded(input, gameProcessed)
	player:Move(Vector3.new(0,0,0))
end
 
UserInputService.TouchStarted:Connect(touchStarted)
UserInputService.TouchEnded:Connect(touchEnded)


1 Like

Maybe you could try using TouchMoved, I think it should work as it contains position data, which you’ll probably need

im sorry but i think you didnt understand what i need is that i want the player to only touch the certain part of the device if its not that part of device then dont execute code if its that part of device then execute code

You can use TouchMoved to do that, just check if the position is on the right side of the screen, e.g

local screenSizeX -- screen size on the X axis
local middle = screenSize/2 -- middle

-- when touch moved fires
local position = touch.Position -- the position
if (position.X >= middle) then
    -- code
end
local UserInputService = game:GetService("UserInputService")

local player = game.Players.LocalPlayer

local touchGui = script.Parent.TextButton

local screenSizeX =UDim2.new(1,0,0,0)

local middle = screenSizeX/2 -- middle

UserInputService.TouchMoved:Connect(function(touch, gameProccessedEvent)

local position = touch.Position -- the position

if (position.X >= middle) then

player:Move(Vector3.new(1,0,0))

end

end)

didnt work :sob:

Sorry for leaving this out, you need to get the screen size from the Camera.ViewportSize

You’re not moving your character correctly, you’re attempting to move the Player object itself, which (as far as I know) doesn’t work. Trying using Humanoid:WalkToPoint on the Humanoid inside of the Player.Character.

local UserInputService = game:GetService("UserInputService")

local player = game.Players.LocalPlayer

local touchGui = script.Parent.TextButton

local cam = workspace.CurrentCamera

local screenSizeX =cam.ViewportSize.X

local middle = screenSizeX/2 -- middle

UserInputService.TouchMoved:Connect(function(touch, gameProccessedEvent)

local position = touch.Position -- the position

if (position.X >= middle) then

player.Characte.Humanoid:Move(Vector3.new(1,0,0))

end

end)

nope didnt work again

You misspelled “Character”

:Move is not a proper function, use MoveTo, and you should probably calculate the position instead of just setting it to 1,0,0

actually i set it 1,0,0 because im making a 2d character movement controller and :Move is a real function why not check the devhub and the misspelled i fixed the spelling and it still doesnt work no errors

local UserInputService = game:GetService("UserInputService")

local player = game.Players.LocalPlayer

local cam = workspace.CurrentCamera

local screenSizeX = cam.ViewportSize.X

local middle = screenSizeX/2 -- middle

UserInputService.TouchStarted:Connect(function(touch, gameProccessedEvent)

local position = touch.Position -- the position

if (position.X >= middle) then

player:Move(Vector3.new(1,0,0))

end

end)

UserInputService.TouchEnded:Connect(function()

player:Move(Vector3.new(0,0,0))

end)

it worked

Misspoke.

I am aware, I’m just saying, if you calculate it, then it could save you much hassle in the future.

if i did calculate how it work?

also latest version of my code

local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local cam = workspace.CurrentCamera
local screenSizeX = cam.ViewportSize.X
local middle = screenSizeX/2 -- middle

UserInputService.TouchStarted:Connect(function(touch, gameProccessedEvent)
	local position = touch.Position -- the position
	if (position.X >= middle) then
		player:Move(Vector3.new(1,0,0))
	end
end)

UserInputService.TouchStarted:Connect(function(touch, gameProccessedEvent)
	local position = touch.Position -- the position
	if (position.X <= middle) then
		player:Move(Vector3.new(-1,0,0))
	end
end)
UserInputService.TouchEnded:Connect(function()
	player:Move(Vector3.new(0,0,0))
end)

Cool. Didn’t know :Move was a function of player, good to know though

Really depends on your game, there a many ways to do it but I wouldn’t worry about it if you don’t want to

oh no it doesnt work in-game on my mobile phone it only works on studio

Did you publish your game so your phone could see the updated changes?

yes i did publish and i know it and now it doesnt even work on studio -_-

Any errors in the dev console while you were testing it on your phone?

there arent any errors too (30)

I gotta go, good luck though :slightly_smiling_face:

i found a way which is disabling the default control script which is overriding it now it works on pc and mobile