Part moves to camera position while setting position to Mouse.hit.p?

Hi, I’m Eli. I was making a build tool, because, why not? And encountered a problem. In my code, I move a part to the mouse.hit.p, to show where the brick will get placed. It flies to the camera. Here is my code.

local SelectedValue = script.Parent:WaitForChild("Selected")
local part = nil
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()



script.Parent.Equipped:Connect(function()
	part = script.Parent:WaitForChild("Selected").Value:Clone()
	part.Parent = workspace
	part.CanCollide = false
	part.Transparency = 0.5
	local Hit = Vector3.new(0,0,0)
	Mouse.Move:Connect(function()
		Hit = Mouse.Hit.p
		part.Position = Hit
		script.Parent:WaitForChild("SelectionBox").Adornee = part
	end)
		
	Mouse.Button1Down:Connect(function()
		
		game.ReplicatedStorage:WaitForChild("PlaceObject"):FireServer(Hit,script.Parent:WaitForChild("Selected").Value)
		
		Mouse.Button1Up:Connect(function()
			
		end)	
	end)							
end)

script.Parent.Unequipped:Connect(function()
	script.Parent:WaitForChild("SelectionBox").Adornee = nil
	if part == nil then
		print("Part is nil!")
	else
		part:Destroy()
		print("Part destroyed")	
	end
end)
1 Like

The mouse is locked onto the part, so the part will be continuously placed at the point where the mouse touches the camera-facing surface. You can set Mouse.TargetFilter to ignore the part; place this line above the Move event.

Mouse.TargetFilter = part

Depending on how your system works, you may have to set it to nil after placing the block.

1 Like

Thanks, I’ll try that out.
30 chars