How to get each stud in a part?

So, we have a part that is 3x3x3.
How do i get the position of each stud in the part?

Let’s say we put parts of size of a stud, each on a position to make a part that is the size of 3x3x3, now i want to do that, but instead, i need to get the studs from the part. Basically cutting in half a part 9 times to get a stud.

Next thing i want to do, is create a part of the size of a stud, and position it to a stud of the part, each by each until it’s completed.

So, how do i get the position of each stud in a part? (No matter the size)

Not sure what you’re trying to explain here, are you trying to split a part into individual studs?

local function GetStudPositions(Part : BasePart) : {}
	local StudPositions = {}
	for X = (Part.Position.X - Part.Size.X / 2) + 0.5, (Part.Position.X + Part.Size.X / 2) - 0.5 do
		for Y = (Part.Position.Y - Part.Size.Y / 2) + 0.5, (Part.Position.Y + Part.Size.Y / 2) - 0.5 do
			for Z = (Part.Position.Z - Part.Size.Z / 2) + 0.5, (Part.Position.Z + Part.Size.Z / 2) - 0.5 do
				table.insert(StudPositions, Vector3.new(X, Y, Z))
			end
		end
	end
	return StudPositions
end

local Part = Instance.new("Part")
local StudPositions = GetStudPositions(Part)
for _, StudPosition in ipairs(StudPositions) do
	print(StudPosition.X, math.round(StudPosition.Y * 10) / 10, StudPosition.Z) --math.round() bit necessary due to long decimals.
end
print(Part.Size.X, math.round(Part.Size.Y * 10) / 10, Part.Size.Z)

image

These were the results for a part of size 3x3x3.

image

2 Likes

This script splits a part into 1x1x1 parts and puts them in and array so you can change the color, size, position etc… .

local box = script.Parent

local function testFunction()
	local center = box.Position
	local size = box.Size
	
	local total = box.Size.X * box.Size.Y * box.Size.Z -- the number of studs inside of the part
	
	local topRight = Vector3.new(center.X + (size.X - 1) / 2, center.Y + (size.Y - 1) / 2, center.Z + (size.Z - 1) / 2)
	
	local rightStuds = {}
	local rightWall = {}
	local splittedBox = {}
	
	for i = size.X, 1, -1 do
		local part = Instance.new("Part", workspace)
		part.Anchored = true
		part.Size = Vector3.one
		part.Position = topRight
		topRight += Vector3.new(-1, 0, 0)
		
		table.insert(rightStuds, part)
	end
	
	for i, v in pairs(rightStuds) do
		
		table.insert(rightWall, v)
		
		local function clonePart(part)
			local clone = part:Clone()
			
			table.insert(rightWall, clone)
			
			clone.Parent = workspace
			clone.Position += Vector3.new(0, -1, 0)
			
			return clone
		end
		
		local clone
		
		for i = 0, size.Y - 2 do
			
			if clone then
				clone = clonePart(clone)
			else
				clone = clonePart(v)
			end
		end
		
	end
	
	for i, v in pairs(rightWall) do
		
		table.insert(splittedBox, v)
		
		local function clonePart(part)
			local clone = part:Clone()

			table.insert(splittedBox, clone)

			clone.Parent = workspace
			clone.Position += Vector3.new(0, 0, -1)

			return clone
		end
		
		local clone
		
		for i = 0, size.Z - 2 do

			if clone then
				clone = clonePart(clone)
			else
				clone = clonePart(v)
			end
		end
	end
	
	local model = Instance.new("Model", box.Parent)
	model.Name = box.Name
	
	for i, v in pairs(splittedBox) do
		v.Parent = model
	end
	
	print(#splittedBox, total)
	
	box:Destroy()
end

testFunction()
1 Like

Thanks, exactly what i needed.