Class · inherits Instance
Mouse
Legacy object that contains members useful for pointer input.
About Mouse
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.
- It is most notable for the
Iconproperty, which changes the cursor's appearance. - It continually raycasts the screen mouse position into the 3D world using
the
TargetFilterproperty, storing the results of the raycast in theHit,Target, andTargetSurfaceproperties. These can be useful for simple cases, butWorldRoot.Raycastshould be used in more complicated raycasting scenarios. Pluginscan usePlugin.GetMouseto get aPluginMouse, which behaves similarly.
-- 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:
This object does not control/restrict pointer movement. For this, see
UserInputService.MouseBehaviorandUserInputService.MouseDeltaSensitivity.If two functions are connected to same input event, such as
Button1Down, both functions will run when the event fires. There is no concept of sinking/passing input, as events don't support this behavior. However,ContextActionServicedoes have this behavior throughBindAction.While a mouse may not be available on all platforms, Mouse will still function on mobile (touch) and console (gamepad), which don't typically have mice or pointer hardware. For explicit cross-platform behaviors, use
UserInputServiceandContextActionService.See Input and Camera for more information on customizing inputs in your experience.
Properties 12
Hit
: CFrameReadOnlyNotReplicatedTheCFrameof the mouse's position in 3D space.This property indicates
CFrameof the mouse's position in 3D space. Note thatMouse.TargetFilterand its descendants will be ignored.Developers can get obtain the position of Hit like so:
local position = mouse.Hit.PositionHit is often used by
Toolsto fire a weapon towards the mouse in third person.Developers looking for the
BasePartthe mouse is pointing at should useMouse.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 ofMouse.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 equalNote, the roll of the
Workspace.CurrentCamerais not used when calculating the orientation of the HitCFrame.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.Icon
: ContentIdThe content ID of the image used as theMouseicon.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.MouseIconEnabledto false.For getting/setting the user mouse icon in experiences, you should use
UserInputService.MouseIcon.Mouse.Iconwill 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 dimensions of the image should not exceed 256 pixels on any axis.
- 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
When using a
PluginMouseretrieved fromPlugin.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 likeMouseEnter,MouseLeave, andMouseButton1Downto 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 otherMouseobjects.IconContent
: ContentThe content of the image used as theMouseicon. Only supports asset URIs.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. This property only supports asset URIs.To hide the cursor entirely, do not use a transparent image – instead, set
UserInputService.MouseIconEnabledto false.For getting/setting the user mouse icon in experiences, you should use
UserInputService.MouseIcon.Mouse.Iconwill 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 dimensions of the image should not exceed 256 pixels on any axis.
- 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
When using a
PluginMouseretrieved fromPlugin.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 likeMouseEnter,MouseLeave, andMouseButton1Downto 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 otherMouseobjects.Origin
: CFrameReadOnlyNotReplicatedACFramepositioned at theWorkspace.CurrentCameraand oriented toward the mouse's 3D position.The origin
Mouseproperty is aCFrameindicating where the mouse originated from. It is positioned at theWorkspace.CurrentCameraand oriented toward the mouse's 3D position.Mouse.UnitRaystarts at the same position as Origin, and extends for a stud in the same direction.local unitRay = mouse.UnitRay local origin = mouse.Origin -- unitRay.Direction = origin.p -- unitRay.Direction ≈ origin.lookVectorTarget
: BasePartReadOnlyNotReplicatedThe object in 3D space themouseis pointing to.The object in 3D space the
mouseis pointing to.Note:
- If
Mouse.TargetFilterhas 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 benil. - Developers looking for the position of the mouse in 3D space should use
Mouse.Hit.
- If
TargetFilter
: InstanceDetermines an object (and its descendants) to be ignored when determiningMouse.HitandMouse.Target.This property determines an object to be ignored by the mouse when calculating
Mouse.HitandMouse.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 affectMouse.HitorMouse.Target.This property can be set to any
Instanceornil, for example:local Players = game:GetService("Players") local Workspace = game:GetService("Workspace") local player = Players.LocalPlayer local mouse = player:GetMouse() mouse.TargetFilter = Workspace.ModelNote that the
Characterof thePlayers.LocalPlayeris ignored by the mouse automatically.TargetSurface
: NormalIdReadOnlyNotReplicatedIndicates theNormalIdof theBasePartsurface at which the mouse is pointing.This property indicates the
NormalIdof theBasePartsurface 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.UnitRay
: RayReadOnlyNotReplicatedARaydirected towards the mouse's world position, originating from theWorkspace.CurrentCameraworld position.The UnitRay property is a
Raydirected toward the mouse's position in 3D space (described byMouse.Hit). It originates from theCFrameof theWorkspace.CurrentCamera. Like all unit rays, it has a distance of 1.ViewSizeX
: intReadOnlyNotReplicatedDescribes the width of the game window in pixels.The ViewSizeX property describes the horizontal component of the game window's size in pixels.
ViewSizeY
: intReadOnlyNotReplicatedDescribes the height of the game window in pixels.The ViewSizeY property describes the vertical component of the game window's size in pixels. This length includes the space used by the topbar.
X
: intReadOnlyNotReplicatedDescribes the X (horizontal) component of the mouse's position on the screen.When detecting changes in the mouse's position on-screen, it is recommended that you use
ContextActionService.BindActionwithUserInputType.MouseMovementorUserInputService.InputChanged, which both describe the position of the mouse using thePosition(aVector3) of anInputObject, 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.Yto produce aVector2representing the mouse's position:local position = Vector2.new(mouse.X, mouse.Y)This property does not fire
Changedor the signal returned fromGetPropertyChangedSignal. Use theMouse.Moveevent instead.Y
: intReadOnlyNotReplicatedDescribes the Y (vertical) component of the mouse's screen position.When detecting changes in the mouse's position on-screen, it is recommended that you use
ContextActionService.BindActionwithUserInputType.MouseMovementorUserInputService.InputChanged, which both describe the position of the mouse using thePosition(aVector3) of anInputObject, 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.Xto produce aVector2representing the mouse's position:local position = Vector2.new(mouse.X, mouse.Y)This property does not fire
Changedor the signal returned fromGetPropertyChangedSignal. Use theMouse.Moveevent instead.
Events 8
Button1Down
Mouse.Button1Down:Connect(function())Fires when the left mouse button is pressed.This event fires when the player presses their left mouse button. Note that this can be accessed from a
Tool; for example, when placed in aLocalScript, the code below printsButton1Downwhenever the left mouse button is pressed.local tool = script.Parent -- Make sure this is a Tool object tool.Equipped:Connect(function(mouse) mouse.Button1Down:Connect(function() print("Button1Down") end) end)You can find out the position of the mouse in world space and if it's pointing at any
BasePartusing theHitandTargetproperties.Button1Up
Mouse.Button1Up:Connect(function())Fires when the left mouse button is released.This event fires when the player releases their left mouse button. Note that this can be accessed from a
Tool; for example, when placed in aLocalScript, the code below printsButton1Upwhenever the left mouse button is released.local tool = script.Parent -- Make sure this is a Tool object tool.Equipped:Connect(function(mouse) mouse.Button1Up:Connect(function() print("Button1Up") end) end)You can find out the position of the mouse in world space and if it's pointing at any
BasePartusing theHitandTargetproperties.Button2Down
Mouse.Button2Down:Connect(function())Fires when the right mouse button is pressed.This event fires when the player presses their right mouse button. Note that this can be accessed from a
Tool; for example, when placed in aLocalScript, the code below printsButton2Downwhenever the right mouse button is pressed.local tool = script.Parent -- Make sure this is a Tool object tool.Equipped:Connect(function(mouse) mouse.Button2Down:Connect(function() print("Button2Down") end) end)You can find out the position of the mouse in world space and if it's pointing at any
BasePartusing theHitandTargetproperties.Button2Up
Mouse.Button2Up:Connect(function())Fired when the right mouse button is released.This event fires when the player releases their right mouse button. Note that this can be accessed from a
Tool; for example, when placed in aLocalScript, the code below printsButton2Upwhenever the right mouse button is released.local tool = script.Parent -- Make sure this is a Tool object tool.Equipped:Connect(function(mouse) mouse.Button2Up:Connect(function() print("Button2Up") end) end)You can find out the position of the mouse in world space and if it's pointing at any
BasePartusing theHitandTargetproperties.Idle
Mouse.Idle:Connect(function())Fired during every heartbeat that the mouse isn't being passed to another mouse event.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.Moveevents.For information on how to obtain the
Mouseobject, please see theMousepage.Developers can find out the position of the mouse in world-space, and if it is pointing at any
BasePartusing theMouse.HitandMouse.Targetproperties.Note, developers are recommended to use
UserInputServiceinstead of theMouseobject in new work.Move
Mouse.Move:Connect(function())Fired when the mouse is moved.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
Mouseobject, please see theMousepage.Developers can find out the position of the mouse in world-space, and if it is pointing at any
BasePartusing theMouse.HitandMouse.Targetproperties.mouse.Move:Connect(function() local position = mouse.Hit.p local target = mouse.Target print(target, position) end)Note, developers are recommended to use
UserInputServiceinstead of theMouseobject in new work.WheelBackward
Mouse.WheelBackward:Connect(function())Fires when the mouse wheel is scrolled backwards.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
Mouseobject, please see theMousepage.Note, developers are recommended to use
UserInputServiceinstead of theMouseobject in new work.WheelForward
Mouse.WheelForward:Connect(function())Fires when the mouse wheel is scrolled forwards.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
Mouseobject, please see theMousepage.Note, developers are recommended to use
UserInputServiceinstead of theMouseobject in new work.
Mouse also has every member of Instance. Deprecated and Roblox-only members are hidden.
Reference text from the Roblox Creator Documentation, licensed CC BY 4.0, reorganized by rbxtoolshub.com · Studio 0.743.0.7431133.