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

How do you detect a mouse's click position?

I'm wondering how it's possible to click somewhere and let's say that I'd like to spawn a brick on the coordinates of the clicked position. How could this be done?

2 answers

✓ Accepted answer

mouse.Hit

mouse = game.Players.LocalPlayer:GetMouse() --this has to be a localscript!

mouse.Button1Down:connect(function() -- button1down fires when the plsyer puts the mouse in a down state
local part = Instance.new("Part")
part.Position = mouse.Hit.p
part.Parent = game.Workspace -- Hit is the position of where the mouse is pointing and .Hit is a CFrame, which is why there is a .p after it.
end)

for more info, check the wiki for mouse

Since we're dealing with the mouse, we'll need to use the mouse class.

A property of mouse is mouse.Hit. This is the position of where the mouse is pointing, which is what you want.

This returns a coordinate frame (CFrame). If you want the part to be exactly where the mouse was clicked use it. Although, if you want to use a Vector3 and make sure it's clear out the way, then use a property of CFrame itself, CFrame.p, shown as mouse.Hit.p.

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