Source: Roblox Creator Hub · CC BY 4.0 · View source · Code samples: MIT Imported 2026-10-03. Formatting adapted for this site.
UI drag detectors
The UIDragDetector instance facilitates and encourages interaction with 2D user interface elements in a game, such as sliders, spinners, and more. Key features include:
Place a
UIDragDetectorunder anyGuiObjectinstance to make it draggable via all user inputs without a single line of code.Choose from several
DragStyleoptions, define how the object responds to motion viaResponseStyle, and optionally apply axis, movement limits, or drag boundaries.Scripts can respond to manipulation of dragged objects to drive logic responses, such as adjusting settings.
UIDragDetectorswork in Studio's edit and play mode as long as you're not using the Select, Move, Scale, or Rotate tools, nor certain plugins or Studio's UI editor tools.
Note
For drag detectors that manipulate 3D objects in a game, such as opening doors and drawers or sliding a part around, see 3D Drag Detectors.
Make UI elements draggable
To make any GuiObject instances draggable, simply add a UIDragDetector as a direct descendant.
In the Explorer window, hover over the
GuiObjectinstance and click the ⊕ button. A contextual menu displays.From the menu, insert a UIDragDetector.
By default, the object will now be draggable in the
LayerCollectorinterface.
Note
Remember that UIDragDetectors only work in Studio if you're not using the Select, Move, Scale, or Rotate tools, nor certain plugins or Studio's UI editor tools.
Customize UI drag detectors
Drag style
UIDragDetectors map cursor motion to calculate proposed 2D motion and/or rotation. Through the DragStyle property, you can choose from different mappings to suit your needs. For example, UIDragDetectorDragStyle.TranslatePlane produces translation in the 2D plane of the LayerCollector, while UIDragDetectorDragStyle.Rotate normally produces a rotation instead of translation.
| Setting | Description |
|---|---|
| [`TranslateLine`](/docs/enum-uidragdetectordragstyle#translateline) | 1D motion along the detector's [`DragAxis`](/docs/uidragdetector#uidragdetector-dragaxis). |
| [`TranslatePlane`](/docs/enum-uidragdetectordragstyle#translateplane) | 2D motion in the plane of the [`LayerCollector`](/docs/layercollector). |
| [`Rotate`](/docs/enum-uidragdetectordragstyle#rotate) | By default, rotation about the absolute center position of the detector's parent [`GuiObject`](/docs/guiobject). If [`ReferenceUIInstance`](/docs/uidragdetector#uidragdetector-referenceuiinstance) is set, rotation happens about that instance's absolute center position. |
| [`Scriptable`](/docs/enum-uidragdetectordragstyle#scriptable) | Calculates desired motion via a custom function provided through [`SetDragStyleFunction()`](/docs/uidragdetector#uidragdetector-setdragstylefunction). |
Drag direction
By default, 2D motion and the associated DragStyle map to the space of the ancestor LayerCollector. However, you may want to change the ReferenceUIInstance or the DragAxis when building different UI components.
| Setting | Description | Default |
|---|---|---|
| [`ReferenceUIInstance`](/docs/uidragdetector#uidragdetector-referenceuiinstance) | A [`GuiObject`](/docs/guiobject) instance whose local space and absolute center position is the reference space and origin for the detector. Setting this reference affects properties such as [`DragUDim2`](/docs/uidragdetector#uidragdetector-dragudim2), [`DragRotation`](/docs/uidragdetector#uidragdetector-dragrotation), and the behavior of [`DragAxis`](/docs/uidragdetector#uidragdetector-dragaxis). | `nil` |
| [`DragAxis`](/docs/uidragdetector#uidragdetector-dragaxis) | [`Vector2`](/docs/vector2) value that defines the axis of movement for the dragged object when [`DragStyle`](/docs/uidragdetector#uidragdetector-dragstyle) is set to [`UIDragDetectorDragStyle.TranslateLine`](/docs/enum-uidragdetectordragstyle#translateline). The axis is defined in the local space of the [`UIDragDetector`](/docs/uidragdetector) unless [`ReferenceUIInstance`](/docs/uidragdetector#uidragdetector-referenceuiinstance) is defined, in which case the axis is defined in that instance's local space. | (1, 0) |
Response to motion
The UIDragDetector.ResponseStyle property specifies how an object's position value is changed by the proposed motion. The custom response styles let you use the resulting UIDragDetector.DragUDim2 and UIDragDetector.DragRotation values as desired, without having the detector's parent execute the proposed motion.
| Setting | Description |
|---|---|
| [`Offset`](/docs/enum-uidragdetectorresponsestyle#offset) | Move by the [`Offset`](/docs/udim#udim-offset) values of the detector's parent's [`GuiObject.Position`](/docs/guiobject#guiobject-position) value. This is the setting by default. |
| [`Scale`](/docs/enum-uidragdetectorresponsestyle#scale) | Move by the [`Scale`](/docs/udim#udim-scale) values of the detector's parent's [`GuiObject.Position`](/docs/guiobject#guiobject-position) value. |
| [`CustomOffset`](/docs/enum-uidragdetectorresponsestyle#customoffset) | The UI element will not move at all, but the [`Offset`](/docs/udim#udim-offset) values of the detector's [`DragUDim2`](/docs/uidragdetector#uidragdetector-dragudim2) will still be updated and the detector's events will still fire, allowing you to respond to drag manipulation however you'd like. |
| [`CustomScale`](/docs/enum-uidragdetectorresponsestyle#customscale) | The UI element will not move at all, but the [`Scale`](/docs/udim#udim-scale) values of the detector's [`DragUDim2`](/docs/uidragdetector#uidragdetector-dragudim2) will still be updated and the detector's events will still fire, allowing you to respond to drag manipulation however you'd like. |
Translation & rotation limits
By default, there are no limits to 2D motion behind the inherent restrictions of the DragStyle. Limits for both minimum and maximum translations and rotations can be declared with the following properties if desired. Additionally, you can define how the dragged object is constrained within the bounds of a specified GuiObject such as a Frame.
| Properties | Description | Default |
|---|---|---|
| [`MinDragTranslation`](/docs/uidragdetector#uidragdetector-mindragtranslation) [`MaxDragTranslation`](/docs/uidragdetector#uidragdetector-maxdragtranslation) | Limits to drag translation in each dimension, defined by a [`UDim2`](/docs/udim2) value. If [`MaxDragTranslation`](/docs/uidragdetector#uidragdetector-maxdragtranslation) is greater than [`MinDragTranslation`](/docs/uidragdetector#uidragdetector-mindragtranslation), translation will be clamped within that range. | \{0, 0\}, \{0, 0\} |
| [`MinDragAngle`](/docs/uidragdetector#uidragdetector-mindragangle) [`MaxDragAngle`](/docs/uidragdetector#uidragdetector-maxdragangle) | Only relevant if [`DragStyle`](/docs/uidragdetector#uidragdetector-dragstyle) is set to [`UIDragDetectorDragStyle.Rotate`](/docs/enum-uidragdetectordragstyle#rotate), or if the functions set through [`SetDragStyleFunction()`](/docs/uidragdetector#uidragdetector-setdragstylefunction) or [`AddConstraintFunction()`](/docs/uidragdetector#uidragdetector-addconstraintfunction) defines a rotation value. If [`MaxDragAngle`](/docs/uidragdetector#uidragdetector-maxdragangle) is greater than [`MinDragAngle`](/docs/uidragdetector#uidragdetector-mindragangle), rotation will be clamped within that range. | 0 |
| [`BoundingBehavior`](/docs/uidragdetector#uidragdetector-boundingbehavior) | Determines the [`UIDragDetector`](/docs/uidragdetector) instance's bounding behavior when its [`BoundingUI`](/docs/uidragdetector#uidragdetector-boundingui) is set. Setting this to [`EntireObject`](/docs/enum-uidragdetectorboundingbehavior#entireobject) bounds the entire dragged UI within the [`BoundingUI`](/docs/uidragdetector#uidragdetector-boundingui), while setting it to [`HitPoint`](/docs/enum-uidragdetectorboundingbehavior#hitpoint) bounds the dragged UI only by the exact hit/grab point and its respective position after translation/rotation. As a convenience, the default of [`Automatic`](/docs/enum-uidragdetectorboundingbehavior#automatic) mimics the [`EntireObject`](/docs/enum-uidragdetectorboundingbehavior#entireobject) behavior for a UI object that's entirely contained by the [`BoundingUI`](/docs/uidragdetector#uidragdetector-boundingui), or else [`HitPoint`](/docs/enum-uidragdetectorboundingbehavior#hitpoint) for a UI object that's partially outside the [`BoundingUI`](/docs/uidragdetector#uidragdetector-boundingui). | [`Automatic`](/docs/enum-uidragdetectorboundingbehavior#automatic) |
Speed adjustments
Through SelectionModeDragSpeed and SelectionModeRotateSpeed, you can fine‑tune the maximum drag/rotate speeds for a detector. Furthermore, through UIDragSpeedAxisMapping, you can fine‑tune the X/Y dimension dragging speeds, based on the detector's SelectionModeDragSpeed.
| Property | Description |
|---|---|
| [`SelectionModeDragSpeed`](/docs/uidragdetector#uidragdetector-selectionmodedragspeed) | Defines the maximum drag speed for translation as a combination of [`Scale`](/docs/udim#udim-scale) and [`Offset`](/docs/udim#udim-offset) of the first ancestor [`ScreenGui`](/docs/screengui) or [`SurfaceGui`](/docs/surfacegui) the [`UIDragDetector`](/docs/uidragdetector) belongs to. |
| [`SelectionModeRotateSpeed`](/docs/uidragdetector#uidragdetector-selectionmoderotatespeed) | Defines the maximum angle per second at which the [`UIDragDetector`](/docs/uidragdetector) can rotate. |
| [`UIDragSpeedAxisMapping`](/docs/uidragdetector#uidragdetector-uidragspeedaxismapping) | Determines the **X**/**Y** dimension dragging speeds, based on the detector's [`SelectionModeDragSpeed`](/docs/uidragdetector#uidragdetector-selectionmodedragspeed). The default is [`XY`](/docs/enum-uidragspeedaxismapping#xy), meaning the **X** and **Y** axis speeds are based off the **X** and **Y** [`Scale`](/docs/udim#udim-scale)/[`Offset`](/docs/udim#udim-offset) values respectively. Alternatives are [`XX`](/docs/enum-uidragspeedaxismapping) and [`YY`](/docs/enum-uidragspeedaxismapping), meaning both the **X** and **Y** axis speeds are based off the **X** ([`XX`](/docs/enum-uidragspeedaxismapping)) or **Y** ([`YY`](/docs/enum-uidragspeedaxismapping)) axis for [`Scale`](/docs/udim#udim-scale), while the [`Offset`](/docs/udim#udim-offset) values still apply to their respective axis. For example, if the first ancestor [`ScreenGui`](/docs/screengui) is sized 800×600 and [`SelectionModeDragSpeed`](/docs/uidragdetector#uidragdetector-selectionmodedragspeed) is `{0.1, 10}, {0.1, 20}` , a setting of [`XX`](/docs/enum-uidragspeedaxismapping) results in an **X**/**Y** drag speed of `80+10`/`80+20`, while [`YY`](/docs/enum-uidragspeedaxismapping) results in `60+10`/`60+20` (note the [`Offset`](/docs/udim#udim-offset) values remain the same in both cases). |
Script responses to clicking and dragging
Through event signals, property changes, Scriptable drag style, and custom functions, scripts can respond to the manipulation of dragged UI elements to drive various settings or make logical decisions, such as sliders that adjust music and sound effect volume separately.
Note
For user-initiated script responses like Event Signals and Scripted Drag Style, you'll most commonly need to put your script code inside a LocalScript, or a Script with RunContext set to Client.
Event signals
Through the following event signals, you can detect when a user starts, continues, and ends dragging an object.
| Event | Description |
|---|---|
| [`DragStart`](/docs/uidragdetector#uidragdetector-dragstart) | Fires when a user starts dragging the object. |
| [`DragContinue`](/docs/uidragdetector#uidragdetector-dragcontinue) | Fires when a user continues dragging the object after [`DragStart`](/docs/uidragdetector#uidragdetector-dragstart) has been initiated. |
| [`DragEnd`](/docs/uidragdetector#uidragdetector-dragend) | Fires when a user stops dragging the object. |
The following slider designates its container as the BoundingUI to limit its movement within the container area, allowing the scale‑based TranslateLine drag to be limited to the full width of the container without extra scripting.
Note
To test this example, download the TransparencySlider.rbxm file, right‑click StarterGui in the Explorer window, select Insert ⟩ Import Roblox Model, and choose the downloaded file.
-- Hierarchy is SliderContainer ⟩ Handle ⟩ UIDragDetector ⟩ (this script)
local sliderContainer = script.Parent.Parent.Parent
local handle = sliderContainer:FindFirstChild("Handle")
local uiDragDetector = handle:FindFirstChildWhichIsA("UIDragDetector")
uiDragDetector.ResponseStyle = Enum.UIDragDetectorResponseStyle.Scale -- Set dragging by scale
uiDragDetector.DragStyle = Enum.UIDragDetectorDragStyle.TranslateLine -- Restricts dragging to line
uiDragDetector.BoundingUI = sliderContainer
-- Initially set container transparency to X scale value of handle
sliderContainer.BackgroundTransparency = 1 - handle.Position.X.Scale
-- Expand handle border to indicate grab start
uiDragDetector.DragStart:Connect(function(inputPosition)
handle:FindFirstChildWhichIsA("UIStroke").Thickness = 6
end)
-- Change transparency by how much it dragged in scale
uiDragDetector.DragContinue:Connect(function(inputPosition)
sliderContainer.BackgroundTransparency = 1 - handle.Position.X.Scale
end)
-- Revert handle border to indicate grab end
uiDragDetector.DragEnd:Connect(function(inputPosition)
handle:FindFirstChildWhichIsA("UIStroke").Thickness = 4
end) Position & rotation changes
In addition to event signals, you can monitor changes to the detector's DragUDim2 and/or DragRotation properties directly.
The following detector has its DragStyle set to Rotate, allowing users to drag the handle around the hue rotator ring, all while detecting changes to drag rotation through Instance:GetPropertyChangedSignal().
Note
To test this example, download the HueRotator.rbxm file, right‑click StarterGui in the Explorer window, select Insert ⟩ Import Roblox Model, and choose the downloaded file.
local handle = script.Parent.Parent -- UI element to drag
local uiDragDetector = handle:FindFirstChildWhichIsA("UIDragDetector")
uiDragDetector.DragStyle = Enum.UIDragDetectorDragStyle.Rotate -- Set drag style to rotate
local function changeHue()
local currAngle = (math.fmod(handle.Rotation, 360)) / 360
if currAngle < 0 then
currAngle += 1
end
handle.BackgroundColor3 = Color3.fromHSV(currAngle, 1, 1)
end
-- Initially set hue to handle rotation
changeHue()
-- Connect function to GetPropertyChangedSignal() of the detector's drag rotation
uiDragDetector:GetPropertyChangedSignal("DragRotation"):Connect(changeHue) Scripted drag style
If you set a detector's UIDragDetector.DragStyle to UIDragDetectorDragStyle.Scriptable, you can provide your own function that takes in a Vector2 of the input position and returns a UDim2 (position) and a float (rotation). The detector will update the object to the computed position/rotation based off of the returns, the DragSpace property, and the DragRelativity property.
By default, the returned UDim2 and float will be the final desired position/rotation in the local space of the detector's parent. Existing translation/rotation limits will still apply, as will boundary limits imposed by a specified BoundingUI instance.
The following example drags a UI element following a sine wave computed by the change in X coordinate input. Note that the detector's DragSpace is set to UIDragDetectorDragSpace.Relative.
local frame = script.Parent -- UI element to drag
local uiDragDetector = frame:FindFirstChildWhichIsA("UIDragDetector")
local initialXValue = 0
local maxHeightChange = 200
local pixelsPerRadian = 75 -- Lower this value to increase frequency
uiDragDetector.DragStart:Connect(function(inputPosition)
initialXValue = inputPosition.X
end)
local function computeSinWaveCoordinate(inputPosition)
local deltaX = inputPosition.X - initialXValue
-- Negative Y delta so that it goes "up" on the screen with positive Y change
local deltaY = -math.sin(deltaX / pixelsPerRadian) * maxHeightChange
return UDim2.fromOffset(deltaX, deltaY)
end
uiDragDetector:SetDragStyleFunction(computeSinWaveCoordinate) Custom constraint function
UIDragDetectors do not have built-in motion rules about grids and snapping, but you can register custom constraint functions to edit the detector's UIDragDetector.DragUDim2 and UIDragDetector.DragRotation before they are applied. For example, you can keep motion on a grid by rounding positions to specific increments, or define allowed areas of motion. Note that this is applied before any existing translation/rotation limits.
Note
Multiple constraint functions can be registered and they will be called in the order of their priority value passed in upon registration, from least to greatest.
The following example utilizes a constraint function that clamps the planar drag into an X/Y grid based on the number of rows and columns. Note that the detector's ResponseStyle is set to UIDragDetectorResponseStyle.Scale and its BoundingUI is set to the grid container.
Note
To test this example, download the GridDrag.rbxm file, right‑click StarterGui in the Explorer window, select Insert ⟩ Import Roblox Model, and choose the downloaded file.
-- Hierarchy is GridContainer ⟩ Handle ⟩ UIDragDetector ⟩ (this script)
local gridContainer = script.Parent.Parent.Parent
local handle = gridContainer:FindFirstChild("Handle") -- UI element to drag
local uiDragDetector = handle:FindFirstChildWhichIsA("UIDragDetector")
uiDragDetector.ResponseStyle = Enum.UIDragDetectorResponseStyle.Scale -- Set dragging by scale
uiDragDetector.DragRelativity = Enum.UIDragDetectorDragRelativity.Relative
uiDragDetector.BoundingUI = gridContainer
local NUM_COLUMNS = 10
local NUM_ROWS = 5
local xScaleIncrement = 1 / NUM_COLUMNS
local yScaleIncrement = 1 / NUM_ROWS
local initialParentPosition = uiDragDetector.Parent.Position
uiDragDetector.DragStart:Connect(function()
initialParentPosition = uiDragDetector.Parent.Position
end)
local function dragToGridOnly(proposedPosition, proposedRotation)
local griddedXScale = math.round(proposedPosition.X.Scale / xScaleIncrement) * xScaleIncrement
local griddedYScale = math.round(proposedPosition.Y.Scale / yScaleIncrement) * yScaleIncrement
return UDim2.fromScale(griddedXScale, griddedYScale), proposedRotation
end
uiDragDetector:AddConstraintFunction(1, dragToGridOnly)