How would i connect these 2 models by these 2 attachments?

I have this model


image
and im trying to connect it to a clone via checking if 2 attachment positions line up (and if they dont collide but ill work on that later)

my code currently isnt working for this, and im not sure exactly how to do the math for this

local Longpath1 = workspace.longpath1
local Longpath2 = workspace.longpath2
local lp1Attachments = {}
local lp2Attachments = {}

for Num, Attachment in ipairs(Longpath1:GetDescendants()) do
	if Attachment:IsA("Attachment") and Attachment.Name:lower() == "connector" then
		table.insert(lp1Attachments, Attachment)
	end
end

for Num, Attachment in ipairs(Longpath2:GetDescendants()) do
	if Attachment:IsA("Attachment") and Attachment.Name:lower() == "connector" then
		table.insert(lp2Attachments, Attachment)
	end
end

local ChosenAttachment : Attachment = lp2Attachments[math.random(1,#lp2Attachments)]
local ChosenAttachment2 : Attachment = lp1Attachments[math.random(1,#lp1Attachments)]
print(ChosenAttachment, ChosenAttachment2)

local Dist = Longpath1.PrimaryPart.CFrame:ToObjectSpace(ChosenAttachment2.WorldCFrame)

local function Test()
	for i = 1, 5 do
		--task.wait(1)
		local Cf1 = ChosenAttachment.WorldCFrame*Dist*CFrame.fromOrientation(0,math.rad(90*(i-1)), 0)
		Longpath1:PivotTo(Cf1)
		
		if ChosenAttachment.WorldPosition == ChosenAttachment2.WorldPosition then
			return true
		end
	end
	return false
end

--task.wait(3)
local Result = Test()
print(Result)
print(Dist)

it either looks like they connected but still prints false

or its just in the center of the other one
image
(they are stacked on each other if you couldn’t tell)

1 Like