How to tile parts from a to b

I want to tile parts from point A to point B, but am unsure how to do the math
Example here, I got 4 points.


And I’m generating the tiles, but they position between the 2 points

The commented out code seems to tile the parts, but obviously doesn’t work on rotated angles. I imagine it should be something like segment * length converted into a vector or something using like ToWorldSpace, but I’ve read the hub on it and still can’t quite understand how it works exactly.

local function CreateWall(segment, length, hitbox, look)
	local Wall = Instance.new("Part")
	Wall.Material = Enum.Material.SmoothPlastic
	Wall.Transparency = 0
	Wall.Orientation = Vector3.new(0, 0, math.rad(90))
	Wall.Size = Vector3.new(
		length,
		12,
		0.2
	)
	
	--local Left = hitbox.Position - Vector3.new(hitbox.Size.X / 2, 0, 0)
	--Wall.CFrame = CFrame.new(
	--	Left + (Vector3.new(0, 0, length) * segment) --- Vector3.new(0, 0, 0.2)
	--)
	--Wall.CFrame += hitbox.CFrame.LookVector * look
	
	Wall.CFrame = hitbox.CFrame + hitbox.CFrame.LookVector * look
	Wall.Anchored = true
	
	return Wall
end

Desired outcome is something like this


Worth noting, ‘hitbox’ is a part that goes from A to B, so can use that to get start and end points

I assume you’re trying to make a tiling placement system. For that you could use math.floor

local part = "part"
local model = "model"

local grid = 1 -- the grid you're trying to achieve. 1 = 1x1, 2 = 2x2 and so on.

local function Snap(part, position)
    local newVec = Vector3.new(
        math.floor(position.X / grid + 0.5) * grid,
        position.Y + part.Size.Y/2, --[[ this will make sure that
        that your part will be on the surface of your mouse hit. You can use "math.floor(position.Y / grid + 0.5) * grid" if you don't want that]]
        math.floor(position.Z / grid + 0.5) * grid
    )

    return newVec
end

part.CFrame = CFrame.new(Snap(part, mouse.Hit.Position)) -- move a single part to desired location
model.PrimaryPart.CFrame = CFrame.new(Snap(model.PrimaryPart, mouse.Hit.Position)) -- move a model to desired location assuming that PrimaryPart isn't nil

this should allow you to move parts or models in a grid, tho I suggest you to visit this link:

it explains everything about grid placement, rotating and more in a more efficient way.
I also suggest to read more about CFrame and Math
https://developer.roblox.com/en-us/api-reference/lua-docs/math

That’s not what I am after. I have the grid placement system already. This is relating to generation of parts after they have been placed. Think of it more as a wall part where I need to split it into several parts and place it along said wall

Assuming I understood what you are trying to do correctly, this is the code I got.

local function createWallSegment(startpoint, endpoint, parent)
	local Segment = Instance.new("Part")
	Segment.Material = Enum.Material.SmoothPlastic
	Segment.Transparency = 0
	Segment.Anchored = true
	
	Segment.CFrame = CFrame.new(startpoint:Lerp(endpoint, 0.5), endpoint)
	Segment.Size = Vector3.new(0.2, 12, (startpoint-endpoint).Magnitude)
	return Segment
end

local colors = {
	BrickColor.Black(),
	BrickColor.Blue()
}
local colorCounter = 1

local function createWall(startpoint, endpoint, segmentSize)
	local wall = Instance.new("Model")
	wall.Name = "Wall"
	
	local dist = (startpoint-endpoint).Magnitude
	local totalSegments = dist/segmentSize
	local increment = 1/totalSegments
	
	for i=0, 1-increment, increment do --last wall needs to be done manually for sizing
		local seg = createWallSegment(startpoint:Lerp(endpoint, i), startpoint:Lerp(endpoint, i + increment))
		seg.Name = "WallSegment"
		
		seg.BrickColor = colors[colorCounter%2 + 1] --Sorry for the weird way of coloring this, I wanted to do it quickly without bulking the code more than necessary
		colorCounter = colorCounter + 1
		
		seg.Parent = wall
	end
	
	local lastWallSize = totalSegments - math.floor(totalSegments)
	local seg = createWallSegment(startpoint:Lerp(endpoint, 1-increment*lastWallSize), startpoint:Lerp(endpoint, 1)) --have to do the last segment manually so it sizes correctly
	seg.Name = "WallSegment"
	seg.BrickColor = colors[colorCounter%2 + 1]
	seg.Parent = wall
	
	
	wall.Parent = game.Workspace
	return wall
end

local a = game.Workspace:WaitForChild("A")
local b = game.Workspace:WaitForChild("B")

createWall(a.Position, b.Position, 3)

For setup I simply had 2 parts named “A” and “B” as children of the workspace which is where the wall gets segmented between. You just specify startpoint endpoint and how long in studs each segment should be and call the second function. My code could probably be a bit cleaner, but I didn’t have much time. If you have any questions feel free to ask.

It essentially works by lerping between start and end to get each segment. Then just placing the part in that segment.

2 Likes

This is close. I don’t want the end section to be a seperate size. It should get the length of the full wall, divide it by 5 (grid size) and size them based on that. So if it means they are 5.6 or whatever then thats fine