I’m currently working on a personal project that involves generating a random map in a specific direction, I’m currently running into some positioning issues when adding pieces.
This is the code I have so far:
local function getRandomMapPiece()
local new = MapPartStorage:GetChildren()[math.random(1,#MapPartStorage:GetChildren())]:Clone()
return new.Name:sub(1,1) ~= "!" and new or getRandomMapPiece()
end
local function isMapPart(part: instance): boolean
return part and part:IsA("Model") and part.Name:sub(1,1) ~= "_" and true or false
end
local function getMapDistance()
local size = 0
for _,mapPart in pairs(MapFolder:GetChildren()) do
if not isMapPart(mapPart) then
continue
end
size += mapPart.PrimaryPart.Size.Z
end
return size
end
function generator:GenerateMap(distance)
self:ClearMap()
for i = 1,(distance and typeof(distance) == "number" and distance or 5) do
local newPiece = getRandomMapPiece()
local pieceSize = newPiece.PrimaryPart.Size.Z
local mapDistance = getMapDistance()
local newDistance = mapDistance + pieceSize
newPiece.Parent = MapFolder
newPiece:PivotTo(basePart.PrimaryPart.CFrame * CFrame.new(0,0,-newDistance))
--[[ -- debug
newPiece.PrimaryPart.BrickColor = BrickColor.Random() ]]
end
self:AddPiece("!End")
return MapFolder
end
This creates this result,
A gap at the start and it goes through the end piece (it should be lining up correctly). I’m not sure if I’m missing something but I can’t seem to figure it out. Any help is much appreciated. Thanks