Preface
The Roblox PlayerMouse is legacy. I imagine it being deprecated within the next few years, and Roblox has been encouraging us to use the newer alternatives they provide, such as ContextActionService and UserInputService. These services do what the PlayerMouse did but better and more. For this reason I made PlayerMouse2. It uses UserInputService and recreates most properties of the old PlayerMouse without actually using it at all.
Documentation
Events
PlayerMouse2.Button1Down ( bool engine_processed )
Fired when the left mouse button is pressed. The engine_processed
argument determines if the engine processed this input.
PlayerMouse2.Button2Down ( bool engine_processed )
Fired when the right mouse button is pressed. The engine_processed
argument determines if the engine processed this input.
PlayerMouse2.Button1Up ( bool engine_processed )
Fired when the left mouse button is released. The engine_processed
argument determines if the engine processed this input.
PlayerMouse2.Button2Up ( bool engine_processed )
Fired when the right mouse button is released. The engine_processed
argument determines if the engine processed this input.
PlayerMouse2.Moved ( bool engine_processed )
Fired when the mouse is moved. The engine_processed
argument determines if the engine processed this input.
PlayerMouse2.WheelScrolled ( int direction, bool engine_processed )
Fired when the mouse wheel is scrolled. The direction
argument is an integer, and will have value 1 if the mouse was scrolled in the forward direction; otherwise -1 if scrolled backwards. The engine_processed
argument determines if the engine processed this input.
PlayerMouse2.WheelDown ( bool engine_processed )
Fired when the mouse wheel is pressed. The engine_processed
argument determines if the engine processed this input.
PlayerMouse2.WheelUp ( bool engine_processed )
Fired when the mouse wheel is released. The engine_processed
argument determines if the engine processed this input.
Properties
int PlayerMouse2.X
The 2D X-coordinate of the mouse.
int PlayerMouse2.Y
The 2D Y-coordinate of the mouse. Accounts for the topbar inset. So subtract by 36 to remove this. This property may be updated in the future to no longer account for the inset.
Vector3 PlayerMouse2.Position
The position of the mouse in the 3D world. The PlayerMouse’s Hit
property is a CFrame, however it is usually only used for its Position
.
Instance<BasePart> PlayerMouse2.Target
The BasePart the mouse is currently hovering over. This value is nil
if the mouse is not pointing at a BasePart, e.g the sky.
CFrame PlayerMouse2.Origin
The CFrame that determines where the mouse originated from. Its position is at the camera’s CFrame.Position
and rotated towards the mouse’s Position
.
Ray PlayerMouse2.UnitRay
A normalized ray whose origin is the camera’s CFrame.Position
, directed towards the mouse Position
.
bool PlayerMouse2.Enabled
Boolean that determines whether or not the mouse icon is enabled.
table PlayerMouse2.TargetFilter
List of instances (and its descendants) to be ignored by PlayerMouse2.Target
and PlayerMouse2.Position
.
Caveats
-
Does not support changing the mouse
Icon
. I could slap a ScreenGui and an ImageLabel however this means when you hover your mouse over the player list, purchase prompts, etc. they will bury the icon. So this seems to be the only caveat so far. A secondary solution would be to just use the mouse for itsIcon
property however that completely goes against the goal of this module. -
Does not support
ViewSizeX
andViewSizeY
. I don’t even consider this a caveat because it just doesn’t seem appropriate for these members to be a part of mouse. The Camera has aViewportSize
property which seems to replicate this.
Code Example
local PlayerMouse2 = require(Path.To.PlayerMouse2.Module)
PlayerMouse2.WheelScrolled:Connect(function(direction)
print(direction == 1 and "The wheel scrolled forward!" or "The wheel scrolled backward!")
end)
PlayerMouse2.Button2Down:Connect(function(engine_processed)
print("Right button down!", engine_processed)
end)
Licensing
This module is licensed under the MIT License.