How to make trees to perlin noise terrain?

I want to make tress spawn randomly like perlin noise

My Terrain Generation

mapxsize = 128
mapysize = 64
mapzsize = 128
seed = math.random(1,100000)
noisescale = 30
amp = 15
psize = 2
positiongrid = {};

for x=0,mapxsize do
for z=0,mapzsize do
for y=0,mapysize do
xnoise = math.noise(y/noisescale,z/noisescale,seed)amp
ynoise = math.noise(x/noisescale,z/noisescale,seed)amp
znoise = math.noise(x/noisescale,y/noisescale,seed)amp
local density = xnoise + ynoise + znoise + y
local densityX = xnoise + ynoise + znoise + x
local densityZ = xnoise + ynoise + znoise + z
if density < 2 and -density < 10 and densityX < 64 and densityZ < 64 and -densityX < 35 and -densityZ < 35 then
local block = Instance.new(“Part”, workspace.terrainFolder)
block.Name = "Block “…x…”, “…y…”, "…z
block.TopSurface = “Smooth”
block.BottomSurface = “Smooth”
block.Material = Enum.Material.SmoothPlastic
block.Anchored = true
block.Size = Vector3.new(psize, psize, psize)
block.CFrame = CFrame.new(x
psize,y
psize,z
psize)

			block.BrickColor = BrickColor.new("Bright green")
			
			
			
		end
		
	end
end
wait()

end

I dont know how to add so that model/tree spawn randomly in the terrain

I have tried doing math.random but my knowledge is too small

3 Likes

Here’s one approach:

repeatedly pick a random block

check if it’s a surface block

check if there’s room above it to place a tree there

place the tree

A simple script for adding trees with your code would be:

Your script circulates over every single terrain piece, so if we would to use math.random we could do something like this:

local TreeChance = math.random(1, 10) -- could be 1 to 10
if TreeChance > 7 then -- 7 in 10 chance it will continue
   -- tree making script here
end

And voila, trees (almost :grin:)

1 Like