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
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.”
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)
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.
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
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.
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!
This is still present and I’m pretty sure this is a syntax typo.
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?
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
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?