Coins dont properly spawn

function getCellPositionsFromPlank(plankModel)
	local primaryPart = plankModel.PrimaryPart
	if not primaryPart then
		warn("Plank model does not have a PrimaryPart: ".. plankModel.Name)
		return {}
	end
	
	local positions = {}
	local partCFrame = primaryPart.CFrame
	local partSize = primaryPart.Size
	
	-- 1. Determine the orientation and length of the plank.
	local numCells
	local stepDirection
	
	if partSize.X > partSize.Z then
		-- X-Oriented plank (e.g., Size is 48x16x16)
		numCells = math.round(partSize.X / CELL_SIZE)
		stepDirection = partCFrame.RightVector
	else
		-- Z-Oriented plank (e.g., Size is 16x16x48)
		numCells = math.round(partSize.Z / CELL_SIZE)
		stepDirection = partCFrame.LookVector
	end
	
	-- 2. Calculate the center of the VERY FIRST cell.
	local totalSpan = (numCells - 1) * CELL_SIZE
	local firstCellCenter = partCFrame.Position - (stepDirection * (totalSpan / 2))
	
	-- 3. Loop and calculate the center of each subsequent cell.
	for i = 0, numCells - 1 do
		local cellCenter = firstCellCenter + (stepDirection * (i * CELL_SIZE))
		table.insert(positions, cellCenter)
	end
	
	print(#positions)
	return positions
end

Each plank is either 48x16x16 or 16x16x48 each “cell” is a cube that is the product of 1/3 of the plank. So really just 1/3 of the plank which is always 16x16x16. I want each cell position in a block. to place my little coin. However when running this function it somtines returns 1 position instead of 3.

meaning it doesnt return all cell positions sometimes only the center. How can I fix this?

2 Likes

What I’m really asking I suppose is to get the center positions of a rectangle (part) that’s able to be divided by cubes of 16 effectively

1 Like
function getCubeCenters(part)
	local p = part
	local cf = p.CFrame
	local s = p.Size
	local positions = {}

	local nx = math.floor(s.X / CELL_SIZE + 0.5)
	local ny = math.floor(s.Y / CELL_SIZE + 0.5)
	local nz = math.floor(s.Z / CELL_SIZE + 0.5)
	if nx == 0 or ny == 0 or nz == 0 then return {} end

	local startOffset = Vector3.new(
		-(nx-1)/2 * CELL_SIZE,
		-(ny-1)/2 * CELL_SIZE,
		-(nz-1)/2 * CELL_SIZE
	)

	for ix = 0, nx-1 do
		for iy = 0, ny-1 do
			for iz = 0, nz-1 do
				local localPos = startOffset + Vector3.new(ix*CELL_SIZE, iy*CELL_SIZE, iz*CELL_SIZE)
				table.insert(positions, cf:PointToWorldSpace(localPos))
			end
		end
	end

	return positions
end

See how that goes.. a bit different. firstCellCenter needs to be correct here.

2 Likes

Oh sorry I never saw this. I fixed the issue a day after I made the post. I was looking for only x and z but forgot y. Along with the part that some sizes werent exactly 16 or 48

1 Like

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