Hi everyone, I’m baking an idea that came to my mind and I’m struggling with CFrames a bit. (At least I think that’s what I should use for this)
Basically I’m making a simple road generator that will use premade meshes like those…
…and just snap them togheter to create a road that starts at point A and ends at point B with as many pieces I decide in the middle of them.
Now, I got the following piece of code…
local SS = game:GetService("ServerStorage")
local roadParts = SS:WaitForChild("RoadParts")
local partsAmount = 5
for i=1,partsAmount,1 do
-- if i==1 then places the start of the road
if i==1 then
start = roadParts.StartFinish.Start:Clone()
start.Parent = workspace.Road
start.Name = tostring(i)
start.Short_Straight.CFrame = CFrame.new(0,1,0)
continue
end
-- Takes all the parts available and put them in a table to assign them a number
parts=roadParts:GetChildren()
partsArray = {}
for i=1, #parts, 1 do
if parts[i]:IsA("Model") then
table.insert(partsArray, parts[i])
end
end
-- Chooses a random road part
chosenPart = partsArray[math.random(1,#partsArray)]
-- Clones it and gives it a name
clone = chosenPart:Clone()
clone.Parent = workspace.Road
clone.Name = tostring(i)
previousRoadPart = workspace.Road:FindFirstChild(tostring(i-1)):FindFirstChildOfClass("MeshPart")
-- Takes the CFrame of the previously spawned road part
referenceCframe = previousRoadPart.After.WorldCFrame.Position
-- Makes the new road part CFrame the same of the previous one plus the difference between the center and the Before point of the mesh
newCFrameVector3= referenceCframe + (clone:FindFirstChildOfClass("MeshPart").CFrame.Position - clone:FindFirstChildOfClass("MeshPart").Before.WorldCFrame.Position)
-- Applies the new CFrame
clone:FindFirstChildOfClass("MeshPart").CFrame = CFrame.new(newCFrameVector3)
end
…which works, but as soon as a turn comes in, the next piece of road is not turned to follow the path.
Now you are gonna say: “It’s obvious, there’s no rotation in your CFrame” and you are right but that’s because I just left the part of the script that worked…
I tried many different methods but none seems to work correctly. I tried PivotTo but doesn’t work, I tried LookAt but doesn’t work, I tried LookAlong but doesn’t work… Maybe I’m doing it wrong idk… A little bit of help on this would mean a lot to me…
Just for reference as I know this might be useful, all the pieces of road have this hierarcy where After and Before are 2 Attachments. After is the attachment that tells you where you should be traveling to and Before is the attachment that tells you where you came from.
I checked that all the 3D arrows of the Attachments point to the right directions, and I’m pretty positive they are unless I’m interpretating them wrongly… In the case of the last picture the After attachment is the one on the left while the Before Attachment is the one on the right.
Thank you in advance to anyone who’s willing to help me out on this!





