Need help creating dynamic segments and placing them accordingly

  1. What do you want to achieve? Keep it simple and clear!

I have a table of “segments” that I’d like to create parts dynamically.

Screenshot 2024-02-13 092828

  1. What is the issue? Include screenshots / videos if possible!

Screenshot 2024-02-13 091909

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I’ve tried a lot of math, pivot points, beams (to see if it can be achieved differently) and research on these forums and other mediums but to no avail.

local segments = {
	{
		length = 5
	},
	{
		length = 10,
		rotateDeg = 45,
		rotateDir = "L"
	},
	{
		length = 7
	}
}

local function getNextMeasurment(currentMeasurment, nextMeasurmentNum)
	local currentMeasurmentSplit = GameScript.split(currentMeasurment.Name, "_")
	local nextMeasurmentNum = tonumber(currentMeasurmentSplit[1]) + nextMeasurmentNum
	local nextMeasurmentSide = currentMeasurmentSplit[3]
	if nextMeasurmentNum > 50 then
		nextMeasurmentSide = nextMeasurmentSide == "L" and "R" or "L"
		nextMeasurmentNum = nextMeasurmentNum - 50
	end
	return {
		part = game.Workspace:WaitForChild("MeasurmentMarkers"):FindFirstChild(nextMeasurmentNum .. "_M_" .. nextMeasurmentSide),
		number = nextMeasurmentNum,
		side = nextMeasurmentSide
	}
end

local function createLine(i, MeasurmentLength, originMeasurment, folder, nextMeasurment, lastPart, lineData)
	local line = Instance.new("Part")
	line.Name = "SegmentName_" .. tostring(i)
	line.Anchored = true
	line.CanCollide = false
	line.Size = Vector3.new(MeasurmentLength + nextMeasurment.part.Size.Z, 2, 1)
	
	line.Position = Vector3.new(originMeasurment.Position.X + (originMeasurment.Size.Y + (originMeasurment.Size.Y / 2)), 1, 1)
	local currentSide = "R"
	local rotateDeg = lineData.rotateDeg
	if rotateDeg and lineData.rotateDir then
		if currentSide == lineData.rotateDir then
			rotateDeg = lineData.rotateDeg * -1
		end
		print("rotatinging", rotateDeg)
		line.CFrame = line.CFrame * CFrame.Angles(0, math.rad(rotateDeg), 0)
	end
	line:SetAttribute("EndMeasurmentNum", tostring(nextMeasurment.number))
	line:SetAttribute("EndMeasurmentSide", nextMeasurment.side)
	line.Material = Enum.Material.Neon
	line.BrickColor = BrickColor.new("New Yeller")
	line.Parent = folder
end

local folder = Instance.new("Folder", workspace)
folder.Name = "Segments"

for i, lineData in ipairs(segments) do
	if i == 1 then
		local nextMeasurment = getNextMeasurment(Measurment20, lineData.length)
		local magnitude = (nextMeasurment.part.Position - Measurment20.Position).Magnitude
		createLine(i, magnitude, Measurment20, folder, nextMeasurment, nil, lineData)
	else
		local lastPart = folder:FindFirstChild("SegmentName_" .. tostring(i - 1))
		local currentMeasurment = game.Workspace:WaitForChild("MeasurmentMarkers"):FindFirstChild(lastPart:GetAttribute("EndMeasurmentNum") .. "_M_" .. lastPart:GetAttribute("EndMeasurmentSide"))
		local nextMeasurment = getNextMeasurment(currentMeasurment, lineData.length)
		local magnitude = (nextMeasurment.part.Position - currentMeasurment.Position).Magnitude
		-- ON R, L 90 POSITIVE, R 90 NEGATIVE
		createLine(i, magnitude, currentMeasurment, folder, nextMeasurment, lastPart, lineData)
	end
end

The “measurements” are parts on the plane that are, let’s say, 200, 5, 1. If I’m starting at marker 20, I’d like to go 5 markers (so I calculate the magnitude) and create a line with that length + width of the marker (5 studs). Next segment I’d like to go 10 markers, calculate that magnitude and rotate it left 45 degrees AND have it connected to the end of the first line. Next segment, I’d like to go 7 markers, calculate the magnitude of that and have it connected to the end of the second line.

1 Like