Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I check if the mouse hovers over a part and when it leaves?

I want to add a SelectionBox to any part the mouse hovers over, and to destroy it once the mouse leaves. I'm unsure how to do this at all, I originally was using UserInputService and raycasting to get the part hovered onto from the camera but the problem with this method was it created a lot of lag since I had to run the function every mouse movement, and I don't know how to detect when the mouse leaves the part.

Is there any simple function I can do this from the mouse? I have many parts so I can't add in like a click detector into every part, was hoping I could do this from just telling the mouse to add a SelectionBox on the part it hovers over, and tells me when it leaves the part. Thanks!

1 answer

✓ Accepted answer

The simple solution is to not deleted the SelectionBox as mouse.Target will be nil allowing you to just set the Adornee to the target.

Example

local localPlr = game:GetService("Players").LocalPlayer
local mouse = localPlr:GetMouse()

local selBox = Instance.new("SelectionBox")
selBox.Parent = localPlr:WaitForChild("PlayerGui")

while true do
	selBox.Adornee = mouse.Target -- Adornee can be set to nil which works well for Target 
	wait()
end

Hope this helps.

1 comment

0

I feel so silly for not doing this. Thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.