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
andContextActionService
, 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:
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.