I need to get the object in the workspace the mouse is hovering over. How can I do this?
You can use the .Target property of Mouse
. For conventional use, apply a .Move signal to perpetually keep track of what the Cursor is hovering over.
1 | local Player = game:GetService( "Players" ).LocalPlayer |
2 | local Cursor = Player:GetMouse() |
3 |
4 | Cursor.Move:Connect( function () |
5 | local Target = Cursor.Target |
6 | if (Target ~ = nil ) then |
7 | print (Target.Name) |
8 | end |
9 | end ) |
I believe using ClickDetector.MouseHoverEnter
will solve your problem, and you could use it in your script like this:
01 | local part = game.workspace.Part --the part you want the mouse to hover over (if it’s in a different place or has another name just change that) |
02 |
03 | local ClickDetector = Instance.new( "ClickDetector" ) --makes a click detector |
04 | ClickDetector.Parent = part --the parent of the click detector (set this to your part name) |
05 | ClickDetector.MaxActivationDistance = 10 --the distance of studs that this script can work from |
06 | ClickDetector.CursorIcon = “ImageId” --this is optional, if you want the mouse/cursor to change then you can put whatever ID in there. (If you don’t want it just delete it) |
07 |
08 | ClickDetector.MouseHoverEnter:Connect( function () |
09 | part.Transparency = . 5 --this is just an example of what will happen when someone hovers, change this to whatever you want to happen when the player hovers over it |
10 | end ) |