[Release] Mouse Module V2

Hi there!

I have previously released a module (see [Release] Mouse Module) to allow developers to use a fake Mouse object to interact with the new UserInputService. I have done this because of this reason:

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.

But this module got outdated and did not work out of the box anymore. So, I decided to revamp it. You can download it here.

Download

Here’s a simple demo of how you could use this module:

And the code that goes with it:

local Debris = game:GetService("Debris")

local Mouse = require(script.MouseModuleV2)

local mouse = Mouse.new()

local leftButtonPressed = false

local function createBlock(color: Color3, temporary: boolean)
	if mouse.Hit then
		local p = Instance.new("Part")
		p.Anchored = true
		p.Size = Vector3.new(2, 2, 2)
		p.CFrame = mouse.Hit
		p.Material = Enum.Material.SmoothPlastic
		p.Color = color
		p.Parent = workspace
		
		if temporary then
			Debris:AddItem(p, 1)		
		end
	end
end

mouse.Button1Down:Connect(function()
	print("Left button pressed!")
	if mouse.Target then
		print(mouse.Target.Name, "was hit!")
	end
	leftButtonPressed = true
end)

mouse.Button1Up:Connect(function()
	print("Left button released!")
	leftButtonPressed = false
end)

mouse.Move:Connect(function()
	if leftButtonPressed then
		createBlock(Color3.fromRGB(220, 220, 220), true)
	end
end)

The module contains the following items:
image

The MouseModuleV2 module is the thing you will actually import when you want to use it. The submodules are used to keep the main module itself a bit cleaner. Since it is a package, you can update it whenever you like after an update is released.

The test code is used to test different aspects of the module. Its test coverage is limited right now, but more tests will be added in the future. You can use this code as reference, so you know what methods / properties are available.

Documentation:

-- Create a new mouse instance
Mouse.new()

-- Get the horizontal and vertical location of the mouse on the screen
Mouse.Location

-- Get the horizontal location of the mouse on the screen
Mouse.X

-- Get the vertical location of the mouse on the screen
Mouse.Y

-- Get the View Size of the screen
Mouse.ViewSize

-- Get the View Width of the screen
Mouse.ViewX

-- Get the View Height of the screen
Mouse.ViewY

-- Get the Target Filters for the raycast of the mouse
Mouse.TargetFilters:Get()

-- Get a ray with the direction of the mouse in 3D space
Mouse.UnitRay

-- Get the origin of the mouse in 3D space
Mouse.Origin

-- Get the position of the mouse in 3D space
Mouse.Hit

-- Get the target instance that the mouse is hovering over
Mouse.Target

-- Signal that the left mouse button was pressed
Mouse.Button1Down

-- Signal that the left mouse button was released
Mouse.Button1Up

-- Signal that the right mouse button was pressed
Mouse.Button2Down

-- Signal that the right mouse button was released
Mouse.Button2Up

-- Signal that the middle mouse button was pressed
Mouse.Button3Down

-- Signal that the middle mouse button was released
Mouse.Button3Up

-- Get the change in x,y of the mouse position since the last recorded position
Mouse:GetDelta(): Vector2

-- Get the ray length of the rays the mouse uses
Mouse:GetRayLength(): number

-- Set the ray length of the rays the mouse uses
Mouse:SetRayLength(rayLength: number): nil

-- Enables the icon of the mouse
Mouse:EnableIcon(): nil

-- Disables the icon of the mouse
Mouse:DisableIcon(): nil

-- Gets the raycast filtertype
Mouse:GetFilterType(): Enum.RaycastFilterType

-- Sets the raycast filtertype
Mouse:SetFilterType(filterType: Enum.RaycastFilterType): nil

-- Gets the raycast's collision group
Mouse:GetCollisionGroup(): string

-- Sets the raycast's collision group
Mouse:SetCollisionGroup(name: string): nil

-- Checks if water is ignored while raycasting or not
Mouse:GetIgnoreWater(): boolean

-- Enables or disables whether water should be ignored while raycasting
Mouse:SetIgnoreWater(ignoreWater: boolean): nil

Let me know if you have any improvements or suggestions. If you run into any problems feel free to post your questions here too.

27 Likes

Ca you Maybe show footage of it being used
For the people who dont want to read the whole thing

Good model though

2 Likes

Good idea, I’ll make sure to add an example.

Could you provide more insight into what usage, features, or direct advantages this provides over the vanilla Mouse?

The Mouse object of player:GetMouse() is not supposed to be used in new work. This module makes use of UserInputService to simulate the Mouse object without actually using it. I’ve also added some documentation.

1 Like

If you want a module that has no backwards compatibility with the original Mouse object. But is simpler to use than UserInputService, you can give my other module a try:

I don’t really see a use for this. If people really wanted, they could just use ContextActionService/UserInputService to ‘recreate’ the Mouse instance, especially if it’s supposed to be a recreation of a deprecated class… I find it simpler to do so in those services, anyways.

4 Likes

Yeah, for most people it might be useless. It was made cuz quite a number of people used my old module, but since it was broken I decided to release a second version.

Unrelated but can someone tell me what “color: Color3, temporary: boolean” means. Like what are the “: Color3 and : boolean” for.

So, this is apart of roblox’s LuaU language. Basically these are called “types” and can help you with scripting, example provided below. This reference uses --!strict

--!strict 
local function Test(arg1: string)
    print(arg1)
end

Test("Hello World!") -- This returns "Hello World!" in the console
Test(workspace) -- This returns nothing since it won't run due to "workspace" being an instance/service

This example isn’t perfect although hopefully you understand a bit more!

2 Likes

The download seems to be broken at the moment, once I get back to my pc I hope to fix it.

EDIT: Should be fixed!

Mouse.Hit it changes only when the player moves the mouse, but when the player turns the camera without moving the mouse, Mouse.Hit remains unchanged, I propose to make it so that when the camera changes its position, the Mouse.Hit also changed or BindToRenderStepped in case something appears in front of the camera

2 Likes

Good suggestion, didn’t realize that. If I have time I’ll add it tomorrow.

Oh so you don’t get warned for unrelated topic and i do? great!

1 Like

Hey! How would I set a TargetFilter with this?

you can access the target filters using TargetFilters property.
That property has these methods to change the filter:

-- Adds a new instance to the list of instances the raycast ignores
mouse.TargetFilters:Add(descendant: Instance): nil

-- Sets the list of instances which will be ignored by the raycast
mouse.TargetFilters:Set(instances: {Instance}): nil

I noticed that mouse.Hit returns nothing if clicking into the sky. Which makes sense but the normal mouse.Hit will typically return a value that is just very far away. Is there a work around for this built in to the module?

I modified the code, it should now behave that way.

Hi there,can the module use on serversided?Thank you.

You cannot use a client-based module for something like the player’s mouse, on the server

1 Like