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

How does mouse.target work?

Asked by
Mystdar 352 Moderation Voter
8 years ago

If I were to to do this:

Player = game.Players.LocalPlayer
mouse = Player:GetMouse()

function DownClick()
    print (mouse.Target.Name)
end 
mouse.Button1Down:connect(DownClick)

Would it print out the name of the part you clicked?

1
Yes. And you could quite easily have tested that yourself in about five seconds. DataStore 530 — 8y
0
Could I really have when i'm posting from a raspberry pi, and don't currrently have anything to test it on? Mystdar 352 — 8y
0
And also if you click on a part in a model, do you click on the model or the part? Mystdar 352 — 8y
0
The Target property will either return the BasePart that the mouse is hovering over or nil. Spongocardo 1991 — 8y
0
Thanks Mystdar 352 — 8y

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

Yes, this will work.

mouse.Target will be the part (not model) that the mouse is hovering over, or nil if the mouse is not hovering over any part (e.g., you click in the sky).

The "or nil" part is very important to remember. The script you have will break if you click in the sky. Always check that mouse.Target exists:

if mouse.Target then
    print(mouse.Target.Name)
else
    print("Sky!")
end
Ad

Answer this question