Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to get the object the mouse is on?

Asked by 4 years ago

I need to get the object in the workspace the mouse is hovering over. How can I do this?

2 answers

Log in to vote
2
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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.

local Player = game:GetService("Players").LocalPlayer
local Cursor = Player:GetMouse()

Cursor.Move:Connect(function()
    local Target = Cursor.Target
    if (Target ~= nil) then
        print(Target.Name)
    end
end)
Ad
Log in to vote
1
Answered by 4 years ago

I believe using ClickDetector.MouseHoverEnter will solve your problem, and you could use it in your script like this:

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)

local ClickDetector = Instance.new("ClickDetector") --makes a click detector
ClickDetector.Parent = part --the parent of the click detector (set this to your part name)
ClickDetector.MaxActivationDistance = 10 --the distance of studs that this script can work from
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)

ClickDetector.MouseHoverEnter:Connect(function()
    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
end)

Answer this question