We use cookies on this site to enhance your user experience

Mouse

Mouse

The Mouse object is the code interface for the computer mouse.

How to get the Mouse?

A Mouse object is not createable, but can be obtained using the Player/GetMouse, Tool/Equipped. The PluginMouse object, which inherits from Mouse can be created using the Plugin Plugin/GetMouse function.

local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer -- from a LocalScript
local mouse = Player:GetMouse()
Tool.Equipped:Connect(function(mouse)
	-- got mouse
end)

Mouse alternatives

There is a degree of overlap between the functionality offered by ContextActionService, UserInputService and the Mouse object.

Mouse, by and large, has been superseded by UserInputService which offers wider additional functionality for interacting with the mouse as well as other input types. For example:

  • UserInputService supports other inputs such as key presses and mobile inputs whereas Mouse does not
  • UserInputService includes additional mouse features such as UserInputService/MouseBehavior and UserInputService/MouseDeltaSensitivity

In most cases developers are advised to use the new UserInputService. However the Mouse object remains supported for a number of reasons.

  • Mouse existed long before UserInputService and a large number of places and gear items are dependent on it
  • The PluginMouse object is still used by plugins accessing the mouse
  • The mouse object is embedded into Tools and is easier to pick up for new developers

Properties

Name Type Description

Hit

CFrame

The DataType/CFrame of the mouse’s position in 3D space.

Content

The content ID of the image used as the Mouse's icon.

CFrame

A DataType/CFrame positioned at the Workspace/CurrentCamera and oriented toward the Mouse's 3D position.

BasePart

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

Instance

If set, the TargetFilter and its descendants will be ignored by the mouse when determining Mouse/Hit and Mouse/Target.

NormalId

Returns the Enum/NormalId of the BasePart surface the mouse is pointing to.

Ray

A DataType/Ray starting at the position of the player’s Workspace/CurrentCamera and travelling towards the Mouse's position in 3D space

int

Represents the width of the game window in pixels.

int

Represents the height of the game window in pixels.

X

int

The X component of the mouse’s position on the screen.

Y

int

The Y component of the mouse’s position on the screen.

Inherited from Instance

Archivable ClassName DataCost Name Parent RobloxLocked

Events

Name Type Description

Fired when the left mouse button is pressed.

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.

Fires when the left mouse button is released.

Fires when the right mouse button is pressed.

Fired when the right mouse button is released.

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

Fired when the mouse is moved.

Fires when the mouse wheel is scrolled backwards.

Fires when the mouse wheel is scrolled forwards.

Inherited from Instance

AncestryChanged Changed ChildAdded ChildRemoved DescendantAdded DescendantRemoving