Build Mode behaving weirdly

I am trying to make a build mode where players are able to build their own cars. However, the part placement system, where the transparent part outline is supposed to follow the player’s mouse is constantly zooming in to the camera, then going to where the player’s mouse is pointing at. However, my goal is for the part to go to where the player’s mouse is pointing at as the player drags their mouse around. How can this problem be resolved?

-- Player Variables
local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local mouse = player:GetMouse()
local connects = {}

--Services
local UserInputService = game:GetService("UserInputService")

--GUI Variables
local selected = script.Parent.Selected
local basic = script.Parent.Basic
local part = basic.Part
local wedge = basic.Wedge
local sphere = basic.Sphere
local cylinder = basic.Cylinder

--Tables
local basics = {"Part", "Sphere", "Wedge", "Cylinder"}

--Non-Declared
local instance

script.Parent.Interface:GetPropertyChangedSignal("Visible"):Connect(function()	
	if script.Parent.Interface.Visible then
		connects.mouseMove = mouse.Move:Connect(onMouseMove)
		connects.part = part.MouseButton1Click:Connect(onPartClick)
		connects.wedge = wedge.MouseButton1Click:Connect(onWedgeClick)
		connects.sphere = sphere.MouseButton1Click:Connect(onSphereClick)
		connects.cylinder = cylinder.MouseButton1Click:Connect(onCylinderClick)
	else
		for _, component in pairs(connects) do
			component:Disconnect()
		end
	end
end)

function onMouseMove()
	local obj = selected.Value
	
	if table.find(basics, obj) ~= nil then
		print(mouse.Hit.p)
		instance.Position = mouse.Hit.p
	end
end

function createTransparentInstance(object)
	local part = Instance.new("Part")
	
	if object == "Part" then
		part.Shape = Enum.PartType.Block
	elseif object == "Sphere" then
		part.Shape = Enum.PartType.Ball
	elseif object == "Cylinder" then
		part.Shape = Enum.PartType.Cylinder
	else
		part = Instance.new("WedgePart")
	end
	
	part.Parent = workspace
	part.Transparency = 0.5
	part.Anchored = true
	
	return part
end

function onPartClick()
	selected.Value = "Part"
	if instance then instance:Destroy() end
	instance = createTransparentInstance(selected.Value)
end

function onSphereClick()
	selected.Value = "Sphere"
	if instance then instance:Destroy() end
	instance = createTransparentInstance(selected.Value)
end

function onWedgeClick()
	selected.Value = "Wedge"
	if instance then instance:Destroy() end
	instance = createTransparentInstance(selected.Value)
end

function onCylinderClick()
	selected.Value = "Cylinder"
	if instance then instance:Destroy() end
	instance = createTransparentInstance(selected.Value)
end
1 Like

If I understood the problem correctly, it might be because the part you are moving affects determining mouse.Hit. You can prevent this by setting mouse.TargetFilter to instance. I’d also recommend simplifying your current onClick functions and have a function they all call, to prevent unnecessary code repetition.

It’d also be a little more efficient to store all your functions in local variables. If you want to define a function below the line where it’s called, you can declare the variable before that line and set its value to the function on any line you want to.

local function onClick(partType)
	selected.Value = partType
	if instance then
		instance:Destroy()
	end
	instance = createTransparentInstance(partType)
	mouse.TargetFilter = instance
end

function onPartClick()
	onClick("Part")
end

function onSphereClick()
	onClick("Sphere")
end

function onWedgeClick()
	onClick("Wedge")
end

function onCylinderClick()
	onClick("Cylinder")
end