How do I find the center point of multiple parts

I have multiple parts in different areas in space, I want to find the exact center of all the parts. I am wanting to do this so I can push them all away from each other.
image - The center point is where they all would come together.

Assuming your Parts are in a Model Or Folder:

for i,p in pairs(Folder:GetChildren()) do
print(i)

end

i represents a number of the items, p represents the object

One jank but simple way to do this without a lot of complex math is to add all of these parts to a Model instance, even if it’s just temporary and use Model.WorldPivot to get the center of the model.

myModel:Model = workspace.Model
print(tostring(myModel.WorldPivot))
3 Likes
local map = workspace.PartFolder

local positionSum = Vector3.new(0,0,0)
local numParts = 0

for i, part in pairs(map:GetChildren()) do
	numParts += 1
	positionSum += part.Position
end

local center = positionSum / numParts

local newPart = Instance.new("Part")
newPart.Anchored = true
newPart.Position = center
newPart.Parent = map

2 Likes

Can you do numParts = i?

text

I see. So this is a vector3 value? Is this what Blue_Jays is saying just in a simple function?

Yes, you will need a Vector3 value to represent the center, you can make it into a function if you’d like:

function returnCenter(partGroup)
		local positionSum = Vector3.new(0,0,0)
		local numParts = 0

		for i, part in pairs(map:GetChildren()) do
			numParts += 1
			positionSum += part.Position
		end
		
		local center = positionSum / numParts

		return center
	end

3 Likes

How do I blast all parts away from the center position, would I simply find which direction the opposing part is from the center, then just multiply that and blast it away, or is it more complex?

function EX()
	OwnerPart.Transparency = 1
	local model = Instance.new("Model")
	for i,v in pairs(ExpParts) do
		v.Parent = model
	end
	local Center = returnCenter()
	for i,v in pairs(ExpParts) do
		local ExplDirection = ...
	end
end

I got this but don’t know how to write expDirection

Seems like exactly what you’d do; once you get the center you can then find the direction to create your forces in for each part

Maybe I just do CFrame:LookAt then I multiply the parts cframe’s look vector? Thats the easiest way I can think of right now… (I might have to make another part and place in the position of the center though, which might be costly)

This is what I was able to come up with…Obviously you can tweak it however you like it but the math works; at the very least you can use the direction vector, which just requires subtracting one CFrame from the other:

function createBlast(partGroup, blastForce)

		local center = getCenter(partGroup)
		

		for _, part in pairs(partGroup:GetChildren()) do
			local directionVector = CFrame.new(center, part.CFrame.p).LookVector
			local attachment = part:FindFirstChild("Attachment") or Instance.new("Attachment", part)

			local velocity = Instance.new("LinearVelocity")
			
			velocity.VectorVelocity = directionVector * blastForce
			velocity.MaxForce = math.huge
			velocity.Attachment0 = attachment
			velocity.Parent = part
			
			part.Anchored = false
			
			task.spawn(function()
				wait(0.1)
				velocity:Destroy()
			end)
			
		end
	end

	createBlast(map, 300)

lol I had the exact same question, heres the topic and solution if it helps:

Use @Blue_Jays’ script to find the center,
Then

local PropelForce = 1 -- How forceful you want the parts to explode away from center

for i,v in pairs(map:GetChildren()) do
	local PropelDirection = -(center - v.Position).Unit * PropelForce
	
	v.Velocity += PropelDirection
end
2 Likes

Actually not a bad idea; I forgot that you can just adjust the velocity property, I don’t use roblox physics a lot lol:

function createBlast(partGroup, blastForce)
	
	local center = getCenter(partGroup)
	

	for _, part in pairs(partGroup:GetChildren()) do
		local directionVector = CFrame.new(center, part.CFrame.p).LookVector
		
		part.Anchored = false
		
		part.Velocity = directionVector * blastForce
		
	end
end

createBlast(map, 100)

I got it working!!
Credit to @Blue_Jays I used part of his code to make mine.


Final Code:

local map = workspace.PartFolder

local positionSum = Vector3.new(0,0,0)
local numParts = 0

for i, part in pairs(map:GetChildren()) do
	numParts += 1
	positionSum += part.Position
end

local center = positionSum / numParts

wait(5)

print("Explode")

for i,v in pairs(map:GetChildren()) do
	v.Anchored = false
	
	local PropelForce = 50 -- How forceful you want the parts to explode away from center

	for i,v in pairs(map:GetChildren()) do
		local PropelDirection = -(center - v.Position).Unit * PropelForce

		v.AssemblyLinearVelocity += PropelDirection
	end
end

I’m also using AssemblyLinearVelocity because I think Part.Velocity is deprecated

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