How to use mouse.Target?

i decided to learn how to use mouse.target on the roblox dev hub, but i found it a bit confusing… so i was wondering if anyone could sort of give me a couple of examples of mouse.target and how to properly use it. thanks!

1 Like

mouse.Target gets the name of what part the mouse is over.

1 Like
local mouse = game.Players:GetMouse().Target

this will return the part the mouse is currently on, whereas mouse.Hit would return the position of the mouse

I suggest using it in a loop

example:

while true do wait()
     local m = game.Players.LocalPlayer:GetMouse()
     print(m.Target,'  ',m.Hit.p)
end
1 Like

don’t use a true loop just do while wait(.1) or wait() do

mouse.Target returns the basepart the mouse is hover over if there is one

detecting goes like:

if mouse.Target then

end

mouse.Target returns a part if there is one

so like

you can do

print(mouse.Target.Name)

or

print(mouse.Target.Position)

and if it returned a target, it will print those

Hello!

It is best to use this function in a local script whereas you can access it better doing this:

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

This function simply gets the Instance that the mouse is pointing to. A simple way to use this is to print the result.

while task.wait(1) do
	print(mouse.Target)
end

If you want to find the position of the Mouse, you can simply use the Mouse.Hit property.

You can read more about these here:
Mouse.Target: Mouse | Documentation - Roblox Creator Hub
Mouse.Hit: Mouse | Documentation - Roblox Creator Hub

Hope this helps! :slight_smile:

1 Like