[Release] Mouse Module

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:

Just wanted to give an update, the new version is almost done but I don’t know if I’ll have time today to finish it. I’ll let you know.

1 Like

This module is now deprecated (but feel free to use it).
Because I have released a new version!
You can check it out using the link below:

1 Like