A script to detect when the cursor of my mouse touch a mesh

Hello! I would like know how to detect when the cursor of my mouse touch a mesh. Can you help me please?

1 Like

What kind of mesh? MeshParts, SpecialMeshes?

Edit: Well, I’m assuming either would work, so…

local plr = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local uis = game:GetService("UserInputService")

local f = {}
function f.raycastMouse()
	local mousePos = uis:GetMouseLocation() -- Get the 2D position of the mouse
	local mouseUnitRay = camera:ScreenPointToRay(mousePos.X, mousePos.Y-36) -- Why add 36 you ask? Because Roblox sucks.
	local mouseRay = Ray.new(mouseUnitRay.Origin, mouseUnitRay.Direction * 100) -- We need to make the ray longer so that it actually reaches anything
	return workspace:FindPartOnRayWithWhitelist(mouseRay, {workspace})
end

function f.mouseTarget()
	local a = f.raycastMouse()
	return a
end

local lastPart = nil

game:GetService('RunService').Heartbeat:Connect(function()
	if lastPart ~= f.mouseTarget() then
		lastPart = f.mouseTarget()
		if lastPart == nil then else
			if lastPart:IsA('MeshPart') or lastPart:FindFirstChildWhichIsA('SpecialMesh') then
				print('Mesh: '..lastPart.Name)
				-- You can put code here, and it'd run whenever a mesh is detected.
				-- Use the lastPart variable to find out what mesh it is, or the parent
				-- of the mesh, depending on the type of mesh.
			end
		end
	end
end)

This should be a decent base for you, it runs a print whenever a mesh is detected, but only once.

It will just raycast your mouse (using the older method) and then find if it matches a type of mesh or has a type of mesh inside.

Good luck!

1 Like

I seriously don’t think it’s neccesary to raycast or even have a loop, you can just use Mouse.Target like this:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

--// A function that runs when mouses moves, better than looping all time
mouse.Move:Connect(function()
   if mouse.Target:IsA("MeshPart") then
      -- do stuff
   end
end)
4 Likes

What do you want say? I didn’t understand why it is deprecated and unreliable.

How is it deprecated, Roblox’s documentation doesn’t say so.

No it is not, maybe it was superseded by UserInputService but in terms of client input, not detecting mouse position, I wouldn’t use Mouse for click detection but I prefer using it for detecting mouse’s Target or Hit position.

This script work only for local script. Can you find another script for normal script?

Well the mouse is only supposed to be returned from a client, don’t think you can use GetMouse() in a server script, but you can send info to serverscripts using Remote Events.

How to detect wich part the mouse touch?

I think I literally specified it here? I even spoonfed you, if you want to know which part then remove the if and just do:

print(mouse.Target)
1 Like

mouse.Target return a string or an instance value?

An instance, of course, mouse.Target.Name would return the name of the object, which you can say a string.

Thank you but there a still a problem. How to get name of this instance? I already tried “mouse.Target.Name” but it don’t work. Can you help me please?

That rather means you used it on the sky.

When using MouseObject.Target and the mouse’s pointing to the sky, it will return nil.

You should check if it’s nil or not. And if it’s not, do whatever you want to.

Alright, here is a solution, let me remind you that .Move runs every time you move your mouse, meaning it could be pointing at the sky which would return nil as @PhoenixRessusection specified, what you would do is basically have a check:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

--// A function that runs when mouses moves, better than looping all time
mouse.Move:Connect(function()
   --// check weather it is nil if not it'll print the instance name mouse's pointing at
   if mouse.Target ~= nil then
      print(mouse.Target.Name)
   end
end)
1 Like