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

How to get position of where mouse clicked?

Asked by 4 years ago

Before I start, I am not trying to get the position of a part a mouse has clicked.

Anyhow, I am attempting to find the position of where a mouse has clicked. I am aware that

local ray = camera:ScreenPointToRay(input.Position.X, input.Position.Y, 0)
local part = workspace:FindPartOnRay(Ray.new(ray.Origin, ray.Direction * 500))

exists, though there's a few problems with it.

1) This script only finds the part of where the mouse has clicked. When I make a script out of this, "part" returns a part, not a position. I could use the position of said part, but it wouldn't be accurate as if I click the tip of, say, baseplate, the position of the baseplate is what it will return and that is not what I want.

2) If I go a few steps back to "input.Position.X" and "Input.Position.Y", this is only XY but I need a Z.

Ultimately, what I'm trying to achieve is create a part of where said XYZ of clicked is. This is the script I currently have:


local tool = script.Parent.FrontPart local UserInputService = game:GetService("UserInputService") local camera = workspace.CurrentCamera UserInputService.InputBegan:Connect(function(input, gpe) if not gpe and input.UserInputType == Enum.UserInputType.MouseButton1 then local ray = camera:ScreenPointToRay(input.Position.X, input.Position.Y, 0) local position = workspace:FindPartOnRay(Ray.new(ray.Origin, ray.Direction * 500)).Position -- this is the previous script but editted if position then -- Blah Blah create part here. end end end)

As you can see this will run some issues. Pretend I want to create a part on the surface of baseplate, what will happen instead is that it will create a part in the center of baseplate. Is there a way to locate the position of when the ray collide with the part, and have it return an accurate position?

0
You could make something follow the mouse, so that when they click, that thing is on the mouse. zandefear4 90 — 4y
0
Im not sure if you can, just a suggestion. zandefear4 90 — 4y

1 answer

Log in to vote
1
Answered by
Nanomatics 1160 Moderation Voter
4 years ago
Edited 4 years ago

If I understand correctly you want to get the position of where the mouse has been clicked on the world?

You can just use:

local LocalPlayer = game.Players.LocalPlayer
local Mouse = LocalPlayers:GetMouse()
Mouse.Button1Down:Connect(function()
    print(Mouse.Hit.p) -- returns the coordinates (x,y,z) of where the mouse has been clicked
end)

Note: I suggest only using UserInputService for keybinding, not for mouse functions

Hope I helped, if you have any questions let me know.

0
Ok thanks! I found the solution earlier, but this is cool as well. xMicro_Canary 37 — 4y
Ad

Answer this question