"Recognizing" groups of parts

I have a model full of parts and i want to separate the parts into a “collection”, i want every part of its group to have the same number as its name, or group them all together (however you wanna help me)


i have this script i wrote here already but im no really sure what to do next?

local Model = workspace.Model
local ModelChildrens = Model:GetChildren()

local function GetDistance(FirstPart, SecondPart, Positive)	
	local Offset = FirstPart.Size.X/2 + SecondPart.Size.X/2
	local Position = FirstPart.CFrame:ToWorldSpace(CFrame.new(Positive and Offset or -Offset,0,0)).Position
	local Distance = (Position - SecondPart.Position).Magnitude
	
	return Distance
end

for FirstIndex, FirstPart in pairs(ModelChildrens) do
	
	for SecondIndex, SecondPart in pairs(ModelChildrens) do -- Negative Offset
		if FirstPart == SecondPart then continue end		
		if GetDistance(FirstPart, SecondPart, false) < 0.1 then else continue end
		
	end
	
	for SecondIndex, SecondPart in pairs(ModelChildrens) do -- Positive Offset
		if FirstPart == SecondPart then continue end		
		if GetDistance(FirstPart, SecondPart, true) < 0.1 then else continue end
		
	end
end

with what i understood, you want every part in a group have the same name? elaborate please

okayy, so you want every part in the model to be seperated in terms of position?

exactly just as shown in the picture, i did a function that returns the ideal position for the next part

this is what the function does


returns the right one if the 3rd value passed to the function is false and the left if it is true

well if the parts are that close, maybe set a small radius to detect? let me see what i can do

after a bit of fiddling, i finally found the way to do it.

group = 0
for i,v in pairs(script.Parent.Model:GetChildren()) do
	v.Size += Vector3.new(0.1, 0, 0.1)
	allowed = true
	group = group + 1
	a = Instance.new("Model")
	a.Name = group
	a.Parent = script.Parent.Model
	v.Parent = a
	currentgroup = a
	for ii,vv in pairs(v:GetTouchingParts()) do
		if vv ~= v then
			if tonumber(vv.Parent.Name) then
				currentgroup = vv.Parent
				break
			else
				vv.Parent = a
			end
		end
	end
	if currentgroup ~= a then
		for i,vvv in pairs(a:GetChildren()) do
			vvv.Parent = currentgroup
		end
		a:Destroy()
		group = group - 1
	end
	v.Size -= Vector3.new(0.1, 0, 0.1)
end

put this script in workspace, and name the model with the parts inside “Model” and this should sort it out for you

edit: not the most efficient code but atleast it works.

before:
image
after:
image

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.