PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

TouchMoved

The TouchMoved event fires when a user moves their finger on a TouchEnabled device.

This event can be used to determine when a user moves their finger while touching the screen of a TouchEnabled device. It can be useful to track whether a user is moving their finger on the screen, as well as where the user is moving their finger.

The code below prints “Touch moved from” the previous Vector2 position "to " the new Vector2 position of the user’s touch on a TouchEnabled device.

local userInputService = game:GetService("UserInputService")

function(touch, gameProcessedEvent)
	local oldPosition = touch.Position - touch.Delta
	print("Touch moved from "..tostring(oldPosition).."to "..tostring(touch.Position))
end

userInputService.TouchMoved:Connect(TouchMoved)

It can be paired with UserInputService/TouchStarted and UserInputService/TouchEnded to determine when a user starts touching the screen, how their finger moves while touching it, and when the they stop touching the screen.

To check if a user’s device is TouchEnabled, and that touch events will fire, see UserInputService/TouchEnabled.

This event only fires when the Roblox client window is in focus. For example, inputs will not be captured when the window is minimized.

As this event only fires locally, it can only be used in a LocalScript.

See also

  • UserInputService/TouchTap
  • UserInputService/TouchTapInWorld
  • UserInputService/TouchLongPress
  • UserInputService/TouchPan
  • UserInputService/TouchPinch
  • UserInputService/TouchRotate
  • UserInputService/TouchSwipe
  • UserInputService/TouchStarted
  • UserInputService/TouchEnded

Parameters

Name Type Default Description

An InputObject instance, which contains information about the user’s input

Indicates whether the game engine internally observed this input and acted on it. Generally this refers to UI processing, so if a button was touched or clicked from this input, gameProcessedEvent would be true. This is also true for input events connected via ContextActionService


Code Samples


The Difference Between TouchTap and TouchLongPress

The code sample below demonstrates the difference between a UserInputService/TouchTap|TouchTap and UserInputService/TouchLongPress|TouchLongPress by creating a GuiObject|GUI Frame that appears when user touches the screen of their device and disappears when the UserInputEvent/TouchEnded|touch ends. When the long press event fires, the GUI doubles in size. Also, the GUI moves to stay centered under the user’s finger when the player moves their finger.

In order for the UserInputService/TouchLongPress|TouchLongPress to fire, the user touch the screen and hold their finger still for a short period of time. Once the touch moves, the long press event will not fire.

In order for the example to work as expected, it should be placed in a LocalScript that is parented to a ScreenGui.

local UserInputService = game:GetService("UserInputService")

-- The parent of this script (a ScreenGui)
local touchScreenGui = script.Parent

-- Create the GUI frame that the user interacts with through Touch
-- events
local touchGui = Instance.new("Frame")
touchGui.Name = "TouchGui"
touchGui.AnchorPoint = Vector2.new(0.5, 0.5)

-- Fires when the touches their device’s screen
local function TouchTap(touch, gameProcessedEvent)
	touchGui.Parent = touchScreenGui
	touchGui.Position = UDim2.new(0, touch.Position.X, 0, touch.Position.Y)
	touchGui.Size = UDim2.new(0,50,0,50)
end

-- Fires when a user starts touching their device's screen and does not
-- move their finger for a short period of time
local function TouchLong(touchPositions, state, gameProcessedEvent)
	touchGui.Size = UDim2.new(0,100,0,100)
end

-- Fires when the user moves their finger while touching their device's
-- screen
local function TouchMove(touch, gameProcessedEvent)
	touchGui.Position = UDim2.new(0, touch.Position.X, 0, touch.Position.Y)
end

-- Fires when the user stops touching their device's screen
local function TouchEnd(touch, gameProcessedEvent)
	touchGui.Parent = nil
	touchGui.Size = UDim2.new(0,50,0,50)
end

-- Only use the Touch events if the user is on a mobile device
if UserInputService.TouchEnabled then
	UserInputService.TouchTap:Connect(TouchTap)
	UserInputService.TouchLongPress:Connect(TouchLong)
	UserInputService.TouchMoved:Connect(TouchMove)
	UserInputService.TouchEnded:Connect(TouchEnd)
end