Reference API Roblox

Engine API

Website

Related

Reference API Roblox

Mouse

Legacy object that contains members useful for pointer input.

This class is not creatable. Instances of this class cannot be created with Instance.new.
Tags: [NotCreatable]

Member index 24

HistoryMember
553Hit: CFrame
645Icon: ContentId
553Origin: CFrame
553Target: BasePart
553TargetFilter: Instance
553TargetSurface: NormalId
553UnitRay: Ray
553ViewSizeX: int
553ViewSizeY: int
553X: int
553Y: int
553target: BasePart
462Button1Down()
462Button1Up()
462Button2Down()
462Button2Up()
462Idle()
553KeyDown(key: string)
553KeyUp(key: string)
462Move()
462WheelBackward()
462WheelForward()
553keyDown(key: string)
inherited from Instance
553Archivable: bool
635Capabilities: SecurityCapabilities
553ClassName: string
553Name: string
553Parent: Instance
635Sandboxed: bool
616UniqueId: UniqueId
553className: string
576AddTag(tag: string): null
573ClearAllChildren(): null
462Clone(): Instance
573Destroy(): null
486FindFirstAncestor(name: string): Instance
486FindFirstAncestorOfClass(className: string): Instance
486FindFirstAncestorWhichIsA(className: string): Instance
486FindFirstChild(name: string, recursive: bool = false): Instance
486FindFirstChildOfClass(className: string): Instance
486FindFirstChildWhichIsA(className: string, recursive: bool = false): Instance
486FindFirstDescendant(name: string): Instance
563GetActor(): Actor
486GetAttribute(attribute: string): Variant
462GetAttributeChangedSignal(attribute: string): RBXScriptSignal
631GetAttributes(): Dictionary
486GetChildren(): Objects
462GetDebugId(scopeLength: int = 4): string
486GetDescendants(): Array
486GetFullName(): string
462GetPropertyChangedSignal(property: string): RBXScriptSignal
641GetStyled(name: string): Variant
576GetTags(): Array
576HasTag(tag: string): bool
486IsA(className: string): bool
486IsAncestorOf(descendant: Instance): bool
486IsDescendantOf(ancestor: Instance): bool
580IsPropertyModified(name: string): bool
573Remove(): null
576RemoveTag(tag: string): null
580ResetPropertyToDefault(name: string): null
573SetAttribute(attribute: string, value: Variant): null
462WaitForChild(childName: string, timeOut: double): Instance
553children(): Objects
553clone(): Instance
573destroy(): null
553findFirstChild(name: string, recursive: bool = false): Instance
553getChildren(): Objects
462isA(className: string): bool
553isDescendantOf(ancestor: Instance): bool
573remove(): null
462AncestryChanged(child: Instance, parent: Instance)
462AttributeChanged(attribute: string)
462Changed(property: string)
462ChildAdded(child: Instance)
462ChildRemoved(child: Instance)
462DescendantAdded(descendant: Instance)
462DescendantRemoving(descendant: Instance)
500Destroying()
553childAdded(child: Instance)

Description

Mouse has been superseded by UserInputService and ContextActionService, which cover a broader scope, are more feature rich, and support cross-platform patterns better. It remains supported because of its widespread use, but you should strongly consider using these alternatives.

The Mouse object houses various API for pointers, primarily for buttons and raycasting. It can be accessed through Player:GetMouse() called on the Players.LocalPlayer in a LocalScript. It is also passed by the Tool.Equipped event.

1
2
3
4
5
6
-- From a LocalScript:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
-- Setting the mouse icon
mouse.Icon = "rbxasset://SystemCursors/Wait"

Note:

History 90

Members 24

Button1Down

Parameters (0)
No parameters.

The Button1Down even fires when the player presses their left mouse button.

This can also be accessed from a Tool. For example, when placed in a LocalScript, the code below prints Button1Down whenever the left mouse button is pressed:

1
2
3
4
5
6
7
local Tool = script.Parent --make sure this is a Tool object

Tool.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		print("Button1Down")
	end)
end)

Developers can find out the position of the mouse in world-space, and if it is pointing at any BasePart, using the Mouse.Hit and Mouse.Target properties.

For information on how to obtain the mouse object, please see the Mouse page.

Note, developers are recommended to use UserInputService instead of the Mouse object in new work.

History 2

Button1Up

Parameters (0)
No parameters.

Fires when the left mouse button is released.

For information on how to obtain the Mouse object, please see the Mouse page.

Developers can find out the position of the mouse in world-space, and if it is pointing at any BasePart using the Mouse.Hit and Mouse.Target properties.

Note, developers are recommended to use UserInputService instead of the Mouse object in new work.

History 2

Button2Down

Parameters (0)
No parameters.

The Button2Down even fires when the player presses their right mouse button.

This can also be accessed from a Tool. For example, when placed in a LocalScript, the code below prints Button2Down whenever the right mouse button is pressed:

1
2
3
4
5
6
7
local Tool = script.Parent --make sure this is a Tool object

Tool.Equipped:Connect(function(Mouse)
	Mouse.Button2Down:Connect(function()
		print("Button2Down")
	end)
end).

Developers can find out the position of the mouse in world-space, and if it is pointing at any BasePart, using the Mouse.Hit and Mouse.Target properties.

For information on how to obtain the mouse object, please see the Mouse page.

Note, developers are recommended to use UserInputService instead of the Mouse object in new work.

History 2

Button2Up

Parameters (0)
No parameters.

Fired when the right mouse button is released.

1
2
3
mouse.Button2Up:Connect(function()
print("button 2 up!")
end

For information on how to obtain the Mouse object, please see the Mouse page.

Developers can find out the position of the mouse in world-space, and if it is pointing at any BasePart using the Mouse.Hit and Mouse.Target properties.

Note, developers are recommended to use UserInputService instead of the Mouse object in new work.

History 2

Hit

TypeDefault
CFrame

This property indicates CFrame of the mouse's position in 3D space. Note that Mouse.TargetFilter and its descendants will be ignored.

Developers can get obtain the position of Hit like so:

local position = mouse.Hit.Position

Hit is often used by Tools to fire a weapon towards the mouse in third person.

Developers looking for the BasePart the mouse is pointing at should use Mouse.Target.

How is Mouse.Hit calculated?

The position of the Hit CFrame is calculated as the point of intersection between the mouse's internal Ray (an extended version of Mouse.UnitRay) and an object in 3D space (such as a part).

The orientation of the Hit CFrame corresponds with the direction of the Mouse.UnitRay.

local unitRayDirection = mouse.UnitRay.Direction
local mouseHitDirection = mouse.Hit.lookVector
-- unitRayDirection ≈ mouseHitDirection
-- the vectors are approximately equal

Note, the roll of the Workspace.CurrentCamera is not used when calculating the orientation of the Hit CFrame.

The mouse's internal ray extends for 1,000 studs. If the mouse is not pointing at an object in 3D space (for example when pointing at the sky), this property will be 1,000 studs away from the Workspace.CurrentCamera.

This property is not replicated. Its interface does not cross the network boundary.
This property is read-only. Its value can be read, but it cannot be modified.

History 4

  • 553 Change Default of Hit from to
  • 486 Change ThreadSafety of Hit from ReadOnly to ReadSafe
  • 462 Change ThreadSafety of Hit from to ReadOnly
  • 47 Add Hit
Tags: [ReadOnly, NotReplicated]

Icon

TypeDefault
ContentId

Icon is a property that determines the image used as the pointer. If blank, a default arrow is used. While the cursor hovers over a GuiButton, this property is temporarily ignored.

To hide the cursor entirely, do not use a transparent image – instead, set UserInputService.MouseIconEnabled to false.

For getting/setting the user mouse icon in experiences, you should use UserInputService.MouseIcon. Mouse.Icon will be deprecated after the new API for plugins to set the mouse cursor is released.

Designing a Cursor

The following guidelines may prove useful when creating your own mouse cursors:

  • The dimensions of the image used determines the size of the cursor.
  • The center of the image is where mouse inputs are issued.
  • The default mouse image is 64x64 pixels, with the mouse taking up 17x24 pixels of space.

System Cursors for PluginMouse

When using a PluginMouse retrieved from Plugin:GetMouse(), you can use the following icons similar to your system's default cursors, such as hands, arrows, I-beams, etc. You can use these with GUI events like MouseEnter, MouseLeave, and MouseButton1Down to provide a consistent studio experience when interacting with certain kinds of GUI components. Note that these only work for studio plugins; they will not work for other Mouse objects.

Look*AssetSuggested Use
rbxasset://SystemCursors/ArrowDefault clicking and selection.
rbxasset://SystemCursors/PointingHandHovering over an active link/button.
rbxasset://SystemCursors/OpenHandHovering over a draggable item.
rbxasset://SystemCursors/ClosedHandDragging an item.
rbxasset://SystemCursors/IBeamHovering in a text field.
rbxasset://SystemCursors/SizeNSHovering over a vertical resize handle.
rbxasset://SystemCursors/SizeEWHovering over a horizontal resize handle.
rbxasset://SystemCursors/SizeNESWHovering over a corner resize handle.
rbxasset://SystemCursors/SizeNWSEHovering over a corner resize handle.
rbxasset://SystemCursors/SizeAllHovering over a multi-direction resize handle.
rbxasset://SystemCursors/SplitNSHovering over a vertical "split" handle.
rbxasset://SystemCursors/SplitEWHovering over a horizontal "split" handle.
rbxasset://SystemCursors/ForbiddenHovering over a locked/forbidden item.
rbxasset://SystemCursors/WaitIndicating an action is in progress.
rbxasset://SystemCursors/BusyIndicating the system is busy.
rbxasset://SystemCursors/CrossHovering over a pinpoint selection area.

* These appearances are approximations – the actual look is dependent on your operating system.

History 5

Idle

Parameters (0)
No parameters.

Fired during every heartbeat that the mouse isn't being passed to another mouse event.

Note, this event should not be used to determine when the mouse is still. As it fires every heartbeat it will fire between Mouse.Move events.

For information on how to obtain the Mouse object, please see the Mouse page.

Developers can find out the position of the mouse in world-space, and if it is pointing at any BasePart using the Mouse.Hit and Mouse.Target properties.

Note, developers are recommended to use UserInputService instead of the Mouse object in new work.

History 2

KeyDown

Parameters (1)
keystring

This event fires when a Key is pressed, with the passed argument being the key that was pressed.

Note:

  • Not all keys generate this event. However, you can get around this with a few of the keys, "/" for example, by using the Mouse.KeyUp event.
  • Some keys generate the same string as other keys.
  • It's possible for the string to be empty (possibly due to "\0" key code).
This event is deprecated. It exists only for backward compatibility, and should not be used for new work. InputBegan should be used instead.

History 5

Tags: [Deprecated]

KeyUp

Parameters (1)
keystring

This event is fired when a Key is released, with the passed argument being the key that was released.

Note:

  • Not all keys generate this event. However, you can get around this with a few of the keys, "/" for example, by using the [[API:Class/Mouse/KeyUp|KeyUp]] event.
  • Some keys generate the same string as other keys.
  • It's possible for the string to be empty (possibly due to "\0" key code).
This event is deprecated. It exists only for backward compatibility, and should not be used for new work. InputEnded should be used instead.

History 5

Tags: [Deprecated]

Move

Parameters (0)
No parameters.

Fired when the mouse is moved.

Note, this event is fired when the mouse's position is updated, therefore it will fire repeatedly while being moved.

For information on how to obtain the Mouse object, please see the Mouse page.

Developers can find out the position of the mouse in world-space, and if it is pointing at any BasePart using the Mouse.Hit and Mouse.Target properties.

mouse.Move:Connect(function()
	local position = mouse.Hit.p
	local target = mouse.Target
	print(target, position)
end)

Note, developers are recommended to use UserInputService instead of the Mouse object in new work.

History 2

Origin

TypeDefault
CFrame

The origin Mouse property is a CFrame indicating where the mouse originated from. It is positioned at the Workspace.CurrentCamera and oriented toward the mouse's 3D position.

Mouse.UnitRay starts at the same position as Origin, and extends for a stud in the same direction.

1
2
3
4
local unitRay = mouse.UnitRay
local origin = mouse.Origin
-- unitRay.Direction = origin.p
-- unitRay.Direction ≈ origin.lookVector

For the position of the Mouse in 3D space, see Mouse.Hit.

This property is not replicated. Its interface does not cross the network boundary.
This property is read-only. Its value can be read, but it cannot be modified.

History 4

Tags: [ReadOnly, NotReplicated]

Target

TypeDefault
BasePart

The object in 3D space the mouse is pointing to.

Note:

  • If Mouse.TargetFilter has been set, the target filter and its descendants will be ignored.
  • When the mouse is not pointing at a BasePart, for example when it is pointing at the sky, Target will be nil.
  • Developers looking for the position of the mouse in 3D space should use Mouse.Hit.
This property is not replicated. Its interface does not cross the network boundary.
This property is read-only. Its value can be read, but it cannot be modified.

History 5

Tags: [ReadOnly, NotReplicated]

TargetFilter

TypeDefault
Instance

This property determines an object to be ignored by the mouse when calculating Mouse.Hit and Mouse.Target. The descendants of the object are also ignored, so it is possible to ignore multiple objects so long as they are a descendant of the object to which this property is set. This property is useful when filtering models containing special effects or decorations that should not affect Mouse.Hit or Mouse.Target.

This property can be set to any Instance or nil, for example:

1
2
3
4
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
mouse.TargetFilter = workspace.Model

Note that the Character of the Players.LocalPlayer is ignored by the mouse automatically.

History 5

TargetSurface

TypeDefault
NormalId

This property indicates the NormalId of the BasePart surface at which the mouse is pointing. This property is derived from the world position of mouse (Mouse.Hit) and the part toward which the mouse is pointing (Mouse.Target).

This property isn't meaningful when the mouse is not pointing at a part, for example when the mouse is pointing at the sky. At the moment, this property is set to 'Right' under these circumstances. Before using this property, check that the mouse's target is not nil.

1
2
3
4
5
6
7
8
9
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
-- Check that there exists a part at which the mouse is pointing
if mouse.Target then
	print("The mouse is pointing to the " .. mouse.TargetSurface.Name .. " side of " .. mouse.Target.Name)
else
	print("The mouse is not pointing at anything.")
end
This property is not replicated. Its interface does not cross the network boundary.
This property is read-only. Its value can be read, but it cannot be modified.

History 4

Tags: [ReadOnly, NotReplicated]

UnitRay

TypeDefault
Ray

The UnitRay property is a Ray directed toward the mouse's position in 3D space (described by Mouse.Hit). It originates from the CFrame of the Workspace.CurrentCamera. Like all unit rays, it has a distance of 1.

1
2
3
4
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
print(mouse.UnitRay.Direction.magnitude) -- Always 1
This property is not replicated. Its interface does not cross the network boundary.
This property is read-only. Its value can be read, but it cannot be modified.

History 4

Tags: [ReadOnly, NotReplicated]

ViewSizeX

TypeDefault
int

The ViewSizeX property describes the horizontal component of the game window's size in pixels.

This property is not replicated. Its interface does not cross the network boundary.
This property is read-only. Its value can be read, but it cannot be modified.

History 4

Tags: [ReadOnly, NotReplicated]

ViewSizeY

TypeDefault
int

The ViewSizeY property describes the vertical component of the game window's size in pixels. This length includes the space used by the topbar.

This property is not replicated. Its interface does not cross the network boundary.
This property is read-only. Its value can be read, but it cannot be modified.

History 4

Tags: [ReadOnly, NotReplicated]

WheelBackward

Parameters (0)
No parameters.

The WheelBackward event fires when the mouse wheel is scrolled backwards. Possible uses for this event include toggling a gun's scope in a first person shooter (FPS) or zooming the player's camera.

This can be used alongside the scrolling forward event, Mouse.WheelForward.

For information on how to obtain the Mouse object, please see the Mouse page.

Note, developers are recommended to use UserInputService instead of the Mouse object in new work.

History 2

WheelForward

Parameters (0)
No parameters.

The WheelForward event fires when the mouse wheel is scrolled forwards. Possible uses for this event include toggling a gun's scope in a first person shooter (FPS) or zooming the player's camera.

This can be used alongside the scrolling backward event, Mouse.WheelBackward.

For information on how to obtain the Mouse object, please see the Mouse page.

Note, developers are recommended to use UserInputService instead of the Mouse object in new work.

History 2

X

TypeDefault
int

When detecting changes in the mouse's position on-screen, it is recommended that you use ContextActionService:BindAction() with UserInputType.MouseMovement or UserInputService.InputChanged, which both describe the position of the mouse using the Position (a Vector3) of an InputObject, instead of using this and related properties.

The X property describes the horizontal component of the mouse's position on the screen. The position is measured in pixels relative to the top left corner, under the topbar. This property can be used in conjunction with Mouse.Y to produce a Vector2 representing the mouse's position:

1
local position = Vector2.new(mouse.X, mouse.Y)

This property does not fire Changed or the signal returned from GetPropertyChangedSignal. Use the Mouse.Move event instead.

This property is not replicated. Its interface does not cross the network boundary.
This property is read-only. Its value can be read, but it cannot be modified.

History 4

  • 553 Change Default of X from to
  • 486 Change ThreadSafety of X from ReadOnly to ReadSafe
  • 462 Change ThreadSafety of X from to ReadOnly
  • 47 Add X
Tags: [ReadOnly, NotReplicated]

Y

TypeDefault
int

When detecting changes in the mouse's position on-screen, it is recommended that you use ContextActionService:BindAction() with UserInputType.MouseMovement or UserInputService.InputChanged, which both describe the position of the mouse using the Position (a Vector3) of an InputObject, instead of using this and related properties.

The Y property describes the vertical component of the mouse's position on the screen. The position is measured in pixels relative to the top left corner, under the topbar. This property can be used in conjunction with Mouse.X to produce a Vector2 representing the mouse's position:

1
local position = Vector2.new(mouse.X, mouse.Y)

This property does not fire Changed or the signal returned from GetPropertyChangedSignal. Use the Mouse.Move event instead.

This property is not replicated. Its interface does not cross the network boundary.
This property is read-only. Its value can be read, but it cannot be modified.

History 4

  • 553 Change Default of Y from to
  • 486 Change ThreadSafety of Y from ReadOnly to ReadSafe
  • 462 Change ThreadSafety of Y from to ReadOnly
  • 47 Add Y
Tags: [ReadOnly, NotReplicated]

keyDown

Parameters (1)
keystring
This event is deprecated. It exists only for backward compatibility, and should not be used for new work. InputBegan should be used instead.

History 4

Tags: [Deprecated]

target

TypeDefault
BasePart
This property is deprecated. It exists only for backward compatibility, and should not be used for new work. Target should be used instead.
This property is not replicated. Its interface does not cross the network boundary.
This property is read-only. Its value can be read, but it cannot be modified.

History 6

Tags: [ReadOnly, NotReplicated, Deprecated]

Settings