Part Generator Positioning Script Not Subtracting Correctly?

I want to make a big part within 3 parts: X, Y, and Z parts. The problem is that (it seems) like it is not subtracting correctly. I have no idea what’s wrong.
Code (Sorry if it seems bad, I’m dumb when it comes to Vector3):

local part = Instance.new("Part")
local coordinates = game.Workspace:WaitForChild("Coordinates")
local setToPositive = function(coordinate)
	if coordinate < 0 then
		local stringed = tostring(coordinate)
		local newnum = string.gsub(stringed, "-", " ", 1)
		local returned = tonumber(newnum)
		return {returned, true}
	else
		return {coordinate, false}
	end
end
local XofX = coordinates:WaitForChild("X").Position.X
local finalXofX = (setToPositive(XofX)[1])
local YofX = coordinates:WaitForChild("X").Position.Y
local finalYofX = (setToPositive(YofX)[1])
local ZofX = coordinates:WaitForChild("X").Position.Z
local finalZofX = (setToPositive(ZofX)[1])
local newVector3ofX = Vector3.new(finalXofX, finalYofX, finalZofX)

local XofZ = coordinates:WaitForChild("Z").Position.X
local finalXofZ = (setToPositive(XofZ)[1])
local ZofZ = coordinates:WaitForChild("Z").Position.Z
local finalZofZ = (setToPositive(ZofZ)[1])
local newVector3ofZ = Vector3.new(finalXofZ, finalYofX, finalZofZ)

local XofY = coordinates:WaitForChild("Y").Position.X
local finalXofY = (setToPositive(XofY)[1])
local YofY = coordinates:WaitForChild("Y").Position.Y
local finalYofY = (setToPositive(YofY)[1])
local newVector3ofY = Vector3.new(finalXofY, finalYofY, finalZofX)

local stringed
local newnum
local returned

local newXofX = Vector3.new(newVector3ofX.X, 0, 0)
local newXofZ = Vector3.new(newVector3ofZ.X, 0, 0)
local xPos = (newXofX - newXofZ)/2
if xPos.X < 0 then
	xPos = (newXofZ-newXofX)/2
end
local XSize = (newVector3ofX - newVector3ofZ).Magnitude

if (setToPositive(XofX)[2]) == true or (setToPositive(XofZ)[2]) == true then
	stringed = tostring(xPos.X)
	newnum = string.gsub(stringed, " ", "-", 1)
	returned = tonumber(newnum)
	xPos = Vector3.new(returned, 0, 0)
end

local newYofX = Vector3.new(0, newVector3ofX.Y, 0)
local newYofY = Vector3.new(0, newVector3ofY.Y, 0)
local yPos = (newYofX - newYofY)/2
if yPos.Y < 0 then
	yPos = (newYofY-newYofX)/2
end
local YSize = (newVector3ofX - newVector3ofY).Magnitude

if (setToPositive(YofX)[2]) == true or (setToPositive(YofY)[2]) == true then
	stringed = tostring(yPos.Y)
	newnum = string.gsub(stringed, " ", "-", 1)
	returned = tonumber(newnum)
	yPos = Vector3.new(0, returned, 0)
end

local newZofX = Vector3.new(0, 0, newVector3ofX.Z)
local newZofZ = Vector3.new(0, 0, newVector3ofZ.Z)
local zPos = (newZofX-newZofZ)/2
if zPos.Z < 0 then
	zPos = (newZofZ-newZofX)/2
end
local ZSize = (newVector3ofX - newVector3ofZ).Magnitude

if (setToPositive(ZofX)[2]) == true or (setToPositive(ZofZ)[2]) == true then
	stringed = tostring(zPos.Z)
	newnum = string.gsub(stringed, " ", "-", 1)
	returned = tonumber(newnum)
	zPos = Vector3.new(0, 0, returned)
end

local FINALVECTOR = Vector3.new(xPos.X, yPos.Y, zPos.Z)
local SIZE = Vector3.new(XSize, YSize, ZSize)

part.Position = FINALVECTOR
part.Size = SIZE
part.Parent = game.Workspace

bro stop ignoring my post :frowning:

1 Like

Are you able to send a picture of the visual result of it? Also you’re trying to make an union, right?

THANK YOU AT LEAST SOMEBODY REPLIED

I’m not making a union, just a part that has its position between the X Y and Z parts.
Photo:
Screen Shot 2021-10-04 at 3.45.39 PM
just look at how far off it is!
What I meant by subtracting was subtracting Vector3 values like

(newZofX-newZofZ)

if you’re trying to position the part between the points you can use the arithmetic average for each coordinate and that should be the center

Already tried that, thing is it subtracts (and divides) the X and Y values, which is NOT what is supposed to happen. I tried getting the average for each coordinate individually, and sticking them together afterwards.

function RetrieveMidPoint(Points)
      local TotalNumber = Vector3.new()
      local TotalDivCount = Vector3.new(#Points, #Points, #Points) 

       for _, Point in pairs(Points) do
             TotalNumber = TotalNumber + Point.Position
       end

       return TotalNumber/TotalDivCount 
end

I don’t know if it’ll work since I’m on mobile but try it

Just asking, is points a list and where should I use this

points is an array of instance points

1 Like

Thanks! Now I just need to fix this…
Screen Shot 2021-10-04 at 4.00.59 PM
I’ll round off the magnitude for the size and see what happens

Works (took off that blank space out there on the part) but why is the part a bit out of place

1 Like

Fixed it, just added half of the size of the part (so to fix the Y coordinate I added half the Y size of the Y part)
EDIT: So the size is broken after moving the X Y and Z parts