Is this the correct way to use to find mobile joystick movement

This does not work for me.

local inputservice = game:GetService("UserInputService")

inputservice.InputBegan:connect(function(i,g)
if i.UserInputType == Enum.UserInputType.Touch then
if i.KeyCode == Enum.KeyCode.Thumbstick1 then
CharacterEvent:FireServer("StopHack")
end
end
end)
4 Likes

If you try to print out the KeyCode, it prints Enum.KeyCode.Unknown and the if-statement will never be true. I recommend what histo_rical said and using those would be a more viable method.

However, there is a problem with this method because if you even tap the screen outside of the bounding box of the joystick you will still get it to fire, which is not what you want. Using TouchMoved would be more realistic as it only fires when the player moves their finger, however, this is still not the best way because what if the player is moving their camera around?

Well there is a fix for this, you can detect whether the player is moving with the TouchMoved event by using the MoveDirection property in Humanoids. It’s set to the Vector3 value of (0, 0, 0) if the player isn’t moving so how can we use that to our advantage? Well, we can check if the Vector3 of that property is not (0,0,0) (they are moving) so we can fix our issue of it running when the player was dragging their finger and moving the camera around.

local inputservice = game:GetService("UserInputService")
local player = game:GetService('Players').LocalPlayer
local character = player.Character

inputservice.TouchStarted:connect(function(touch, gameProcessed)
	print('Started')
end)

inputservice.TouchEnded:Connect(function(touch, gp)
	print('Ended')
end)

inputservice.TouchMoved:Connect(function(touch, gp)
	if character.Humanoid.MoveDirection ~= Vector3.new() then
		print('Moved!')
	end
end)

Try this out and it should work well! I actually learned something new myself doing this so I hope you can learn something from it too! This is a much better method than using the PlayerControls method or using the method you tried earlier!

16 Likes

Very clean there, I’ve learnt something from this aswell. Thank you for that.

5 Likes