[Problem Found]Buildings generating in weird places(RANDOMLY GENERATED PLANET)

Hello, I’m currently having a problem where buildings are generating in weird places on a randomly generated planet.

I want the buildings to generate at the planets ground level.

Here’s the problem I’m currently getting.


I’m using DutchDeveloper’s Cube generation script to generate the buildings

function getRandomCFrame(object)
	local X = object.Size.X/2
	local Y = object.Size.Y/2
	local Z = object.Size.Z/2
	local RX = math.random(-X,X)
	local RZ = math.random(-Z,Z)
	local RY = math.random(-Y,Y)
	return object.CFrame*CFrame.new(RX,RY,RZ)
end

function generateSimpleBuildings(planet)
	local buildings = game.ReplicatedStorage.Civilizations.Simple:GetChildren()
	
	-- Create a folder to store all the buildings
	
	local buildingsFolder = Instance.new("Folder", planet)
	buildingsFolder.Name = "Buildings"
	
	for i = 20,0,-1 do
		local randomBuilding = buildings[math.random(1,#buildings)]:Clone()
		randomBuilding.Parent = buildingsFolder
		local randomCF = getRandomCFrame(planet.Outer)
		randomBuilding:SetPrimaryPartCFrame(randomCF
			
		)
		
		-- ROTATE BUILDINGS
		randomBuilding:SetPrimaryPartCFrame(CFrame.new(randomBuilding.PrimaryPart.Position, -planet.Outer.Position))
		
		wait(1)
	end
	
end

How can I fix this?

1 Like

Are the buildings themselves all randomly generated meaning are the buildings different heights or shapes?

No, it chooses a random building from a folder from ReplicatedStorage, and places it inside the planet. No properties are changed except the CFrame or Position.

My goal is to make the buildings generate on the planet. Not in it or out of it.

I made some points that the buildings can generate on, it works very well instead of having to do randomly generated positions.