PcoWSkbVqDnWTu_dm2ix
Collapse Sidebar

TouchStarted

The TouchStarted event fires when a user places their finger on a UserInputService/TouchEnabled|TouchEnabled device, beginning touch input with the device.

This event can be used to determine when a user starts touching the screen of their device. It can be paired with UserInputService/TouchEnded to determine when a user starts and stops touching the screen.

The touch input object is the same input object throughout the lifetime of the touch. So comparing InputObject|InputObjects when they are touch objects is valid to determine if it is the same finger.

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/TouchMoved
  • UserInputService/TouchPan
  • UserInputService/TouchPinch
  • UserInputService/TouchRotate
  • UserInputService/TouchSwipe
  • 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


Tracking Touches

This example demonstrates how to use touch input events to drag a GUI element while a player touches and drags across their screen.

The touch InputObject is the same input object throughout the lifetime of the touch. So comparing input objects when they are touch objects is valid to determine if it is the same touch as the input starts, changes, and ends.

The example starts tracking a drag once the touch is registered by the UserInputService/TouchStarted|TouchStarted event. It continues to update as that touch moves, tracking it’s position relative to start position to move a GUI until a UserInputService/TouchStarted|TouchEvent event fires for that touch.

local UserInputService = game:GetService("UserInputService")

local dragging
local dragInput
local dragStart
local startPos

local gui = script.Parent

local function touchStarted(input, gameProcessed)
	if not dragging then
		dragging = true
		dragInput = input
		dragStart = input.Position
		startPos = gui.Position
	end
end

local function update(input, gameProcessed)
	if input == dragInput  and dragging then
		local delta = input.Position - dragStart
		gui.Position = UDim2.new(startPos.X.Scale, startPos.X.Offset + delta.X, startPos.Y.Scale, startPos.Y.Offset + delta.Y)
	end
end

local function touchEnded(input, gameProcessed)
	if input == dragInput then
		dragging = false
	end
end

UserInputService.TouchStarted:Connect(touchStarted)
UserInputService.TouchMoved:Connect(update)
UserInputService.TouchEnded:Connect(touchEnded)