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

How do you detect a mouse's click position? [closed]

Asked by
Acheo 230 Moderation Voter
8 years ago

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?

Closed as Not Constructive by AmericanStripes

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

2 answers

Log in to vote
2
Answered by
theCJarmy7 1293 Moderation Voter
8 years ago

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

Ad
Log in to vote
1
Answered by 8 years ago

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.