Trying to create a part on the players mouse position on click

I’ve tried to write a code out for this, however, it places it at the world origin. Any help?

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

Mouse.Button1Up:Connect(function()
	print("Mouse position is ", Mouse.Hit.p)
	NewPart = Instance.new("Part", game.Workspace)
	NewPart.Position = Vector3.new(Mouse.Hit.p)
end)

2 Likes

Well here’s a question I have,
Do you want the part to only be visible to the client or shared to the server?
(will help with more info, which is ^^^)

1 Like

Hey, I would like it shared to the server. Thank you for helping

2 Likes

You could try doing this:

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

Mouse.Button1Up:Connect(function()
	local pos = Mouse.Hit.Position
	print("Mouse position is ", pos)
	local NewPart = Instance.new("Part", game.Workspace)
	NewPart.Position = pos
end)

Tried this on the client and it seemed to work.

1 Like

Okay, I’ll be making a client and server with helpful comments and tips throughout it, please await my response.

1 Like

You can’t insert a part on the client by the way, it wont replicate to the server.
Let me rephrase,
You can but it won’t be replicated**

1 Like

Yup, I’m aware that it wont be replicated and it will only be visible to the client.

If you want this to replicate to the server, you can do so with RemoteEvents.

For my example, I made a RemoteEvent called ‘CreatePart’ in ReplicatedStorage.

-- local

local PS = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")

local createPart = RS.CreatePart
local player = PS.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local mouse = player:GetMouse()

mouse.Button1Down:Connect(function()
	createPart:FireServer(mouse.Hit.Position)
end)
-- server 

local RS = game:GetService("ReplicatedStorage")

local createPart = RS.CreatePart

createPart.OnServerEvent:Connect(function(player, pos)
	local part = Instance.new("Part")
	part.Position = pos
	part.Parent = workspace
end)

4 Likes

Okay! I will await your response.

Thanks for replying! I will test this out now.