Detect location where mouse is pointed on 3 axis

I am trying to make a function, that when the player hits a keyboard hotkey, (example: R-key), the game will detect the location of the closest on-surface position in the exact direction where the player’s mouse is pointed, but I have no clue what function I would use for that detection, can anyone help?

Thanks for reading

You can get the player’s mouse via GetMouse then use Mouse.Hit

2 Likes

Could you show me an example of how this would be used?

There is a pretty good example on the link I posted for Mouse.Hit. Let me know if you’re still confused after testing that out.

1 Like

I’m not sure I get that right, also, how would I add that I make a brick tween in that location, visible to a specific team?

I believe this answer your question: https://youtu.be/3i8ECQCCa1I

1 Like

With the mouse.hit function, is it possible to detect where exactly the mouse on the part is hovering?

1 Like

I noticed, mouse.hit will give me the exact location of the mouse in the workspace, is it possible for me to save that location to a local and then create a part on that location?

1 Like

Yeah you can get the mouse position with

Mouse.Hit.p

1 Like

if I want to create a part in that location, would i do the following?

local part = Instance.new(“Part”)
part.Parent = workspace
part.Position = Vector3.new(Mouse.Hit.p)

I don’t think you need vector3 in that place but yes it will work.

1 Like

So would i just do the following then?
part.Position = mouse.hit.p

Yes if it did not work use the Vector3 method.

1 Like

Use this example to replace the build part with the transport

FireServer

Create tool like image

– Script

local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local TransferPartEvent = Instance.new(“RemoteEvent”, ReplicatedStorage)
TransferPartEvent.Name = “TransferPartEvent”

local Part = game.Workspace.Part

local function onTransferPartFired(player, Location)
print(player.Name, “wants to Transfer a part”)

Part.Position = Location

end

TransferPartEvent.OnServerEvent:Connect(onTransferPartFired)

– LocalScript

local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local TransferPartEvent = ReplicatedStorage:WaitForChild(“TransferPartEvent”)
local tool = script.Parent
tool.Equipped:Connect(function(mouse)
mouse.Button1Down:Connect(function()
print(“Button1Down”)

	 local Location = mouse.Hit.p
     TransferPartEvent:FireServer(Location)
	
end)

end)

Mouse.Hit returns a CFrame value that can be applied to 3D objects.

1 Like