PlayerMouse2 - an elegant, modern alternative to PlayerMouse

Preface

The PlayerMouse is legacy. Roblox has for years now suggested us to move to UserInputService and ContextActionService for handling input for years now. What does this module do? Use the former.

The API is meant to give a similar feeling to the PlayerMouse, but some of the names are a little more modern.

While a lot of the code is coincidentally the same, I personally would consider this a fork of starmaq’s Custom Mouse Module since some of it ended up being based on it anyway, and again the coincidental similarities.

Properties that describe the viewport size are intentionally not present in this module due to it feeling misplaced under a mouse interface. (camera.ViewportSize anyone?)

Why this module?

I am aware there are similar modules, but they have flaws like not cleaning up connections properly, or are unmaintained, etc.

This module aims to fix all that.

Source / Documentation

You can get the source code for this as well as the documentation:

Source

if you use roblox-ts here is the type definition file, yes i use this module in a game that uses roblox-ts and it works good
interface PlayerMouse2 {
	Delta: Vector2;
	Hit: CFrame;
	IconEnabled: boolean;
	Normal: Vector3;
	Origin: CFrame;
	Position: Vector2;
	Sensitivity: number;
	Target?: BasePart;
	TargetFilter: Instance[];
	UnitRay: Ray;

	LeftPressed: RBXScriptSignal;
	LeftClicked: RBXScriptSignal;
	LeftReleased: RBXScriptSignal;
	RightPressed: RBXScriptSignal;
	RightClicked: RBXScriptSignal;
	RightReleased: RBXScriptSignal;
	Moved: RBXScriptSignal;
	WheelScrolled: RBXScriptSignal;
	WheelReleased: RBXScriptSignal;
	WheelPressed: RBXScriptSignal;
	WheelClicked: RBXScriptSignal;
}

declare const PlayerMouse2: PlayerMouse2;
export = PlayerMouse2;

Documentation

16 Likes

This is pretty useful. The events are good too.
One thing is that I think the names sound a bit weird?

RightClick() would’ve been probably better than RightClicked() at least in my opnion, but it seems really good other than that.

No, because RightClicked is an event. The “ed” implies it happened in the past.

1 Like

I fixed a bug where the Hit.Position would not work correctly sometimes, it was related to the target filter. I was inserting to the PlayerMouse2 table itself, and not PlayerMouse2.TargetFilter.

There should not be any bugs anymore, as this stuff has been tested, just a few oversights but they should be fixed. If there happens to be more please let me know, and optionally file a pull request with the fix.