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.
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:
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.
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