[Release] Mouse Module

Since Roblox recommends not to use the Mouse object, I thought it would be a good idea to create a simple module which simulates the Mouse object.
To create this I used UserInputService and the Camera object.
Feel free to make changes wherever you like and let me know if you run into any issues or if you would like to add new functionality to this module.

Example

local MouseMod = require(script.MouseModule)

local Players = game:GetService("Players")

local mouse = MouseMod.new()
local plr = Players.LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
mouse:SetTargetFilter(character)

local pos = mouse:GetPosition()
print(pos)
local part, position = mouse:CastRay()
print(part, position)
if part and part:IsA("BasePart") then
	local clone = part:Clone()
	clone.CFrame = CFrame.new(position)
	clone.Material = Enum.Material.Neon
	clone.Parent = workspace
end

Download:
https://www.roblox.com/library/4835733390/Mouse-Module

50 Likes

The Mouse Instance is deprecated? I used it for all the years I was scripting. However, you made a great job!

5 Likes

It isn’t deprecated though, at least not according to the wiki: “In most cases developers are advised to use the new UserInputService. However the Mouse object remains supported for a number of reasons.”

9 Likes

yeah, it’s not deprecated as far as i know, there’d probably be a notice

also it would break every game imaginable if it was deprecated

1 Like

Oh, people told me it was, guess I was wrong, but if it gets deprecated, here’s an alternative.

1 Like

For what it’s worth, deprecated doesn’t mean “will not work”, so much as “not recommended for use”.

7 Likes

I modified it in the post, thanks.

2 Likes

Thanks for making this! I’m not sure if the module is being maintained anymore, but the method used for handling rays has been deprecated, and brings about the error attempt to multiply a Vector3 with an incompatible value type or nil - Client - Mouse2020:35. Also there isn’t any built in way to detect if the mouse has moved, but I created an efficient way to do so in the context of a tool:

local connection
local lastMousePos = nil

tool.Equipped:Connect(function()
	connection = RunService.Heartbeat:Connect(function()
		local currentMousePos = mouseModule:GetPosition()

		if currentMousePos ~= lastMousePos and lastMousePos ~= nil then -- after actual mouse move
			mouseMoved()
		end

		lastMousePos = mouseModule:GetPosition()
		
	end)
end)

tool.Unequipped:Connect(function()
	connection:Disconnect() -- remove from taskscheduler (saves memory and reduces huge lag spikes)
end)
1 Like

Preferably the .Move equivalent would be able to have functions :Connect() to it or be able to be sent and run through an argument. I think a bindable event might work from a glance at the similarities to RBXScriptSignals

I’ll add it, I don’t really maintain this project anymore but I’ll let you know when it’s updated.
There’s a simpler solution to detect if the mouse is moved though.


(Just noticed it only works when the user is fully zoomed in or uses shift lock, let me find a better way)

1 Like

I’ve updated it.
Small example here:

local MouseMod = require(script.MouseModule)
local mouse = MouseMod.new()

local plr = game:GetService("Players").LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
mouse:SetTargetFilter(character)

while true do
	local pos = mouse:GetPosition()
	local raycastResult = mouse:CastRay()
	if raycastResult then
		local clone = raycastResult.Instance:Clone()
		clone.Size = Vector3.new(1, 1, 1)
		clone.CFrame = CFrame.new(raycastResult.Position)
		clone.Material = Enum.Material.Neon
		clone.Parent = workspace
	end
	print(mouse:GetDelta())
	wait(0.2)
end
1 Like

Is there a way to add multiple objects when i use SetTargetFilter function?

I’m not sure if you can just pass a table, if not I’ll take a look at it tomorrow.

I cant figure out what data type you are wanting to pass through with the
Mouse:SetFilterType() I cant figure out how to put something in there because when I pass a string or Enum I get an error.

1 Like

I just looked through the code, you can pass it a RaycastFilterType enum.
This could be Enum.RaycastFilterType.Blacklist or Enum.RaycastFilterType.Whitelist.
You’ll need to update the module with the new version though, I fixed some mistakes.
Thanks for letting me know!

1 Like

Hey! Have you updated the module? If not

This is still present and I’m pretty sure this is a syntax typo. :sweat_smile:

Theres also this

function Mouse:SetTargetFilter(object)
	local dataType = typeof(object) -- dataType is now a string
	if typeof(dataType) == "Instance" then --this means your checking what is dataType which has turned into a string
		self.filterDescendants = {object}
	elseif typeof(dataType) == "table" then
		self.filterDescendants = object
	else
		error("object expected an instance or a table of instances, received: "..dataType)
	end
end

Your checking typeof twice meaning the second check it would output as a string. Here is the fixed version:

function Mouse:SetTargetFilter(object)
	local dataType = typeof(object)
	if dataType == "Instance" then
		self.filterDescendants = {object}
	elseif dataType == "table" then
		self.filterDescendants = object
	else
		error("object expected an instance or a table of instances, received: "..dataType)
	end
end

Also is there an alternative rather than returning nil when aiming at the sky?

2 Likes

Uhh, sorry I think it was 11PM when I updated it, made a bunch of silly mistakes.
Thanks for letting me know.
I’ve updated the :SetFilterType method, since the old version was complete garbage and didn’t even work.

function Mouse:SetFilterType(filterType)
	local filterTypes = Enum.RaycastFilterType:GetEnumItems()
	if table.find(filterTypes, filterType) then
		self.filterType = filterType
	else
		error('Invalid raycast filter type provided')
	end
end

And I’ve updated the :SetTargetFilter method as you said, that’s just something I overlooked, thank you for letting me know.

I’ve now updated the module, let me know if you run into any more issues :smiley:

1 Like

I love the idea, something I was looking for! Would love to see some documentation for the module, since there are a lot of functions. Would you mind creating it? :wink:

1 Like

you should probably update the post’s example, it took me a few minutes to fix errors with relating to the raycastResult

1 Like

If I have some time tomorrow, I’ll see if I can revamp all the code and publish it with some documentation :slight_smile: