I was creating a terrain generator today and I could not figure out how to make there be trees on the grass and snow areas. Here is my current script that generates the land:
local Seed = 1002
local freq = 150
local Smoothness = 200
local Size = 1000
local PartSize = 5
for X = 0,Size,PartSize do -- X Loop
for Z = 0,Size, PartSize do -- Z Loop
local p = Instance.new("Part",workspace.Map)
p.Anchored = true
p.Shape = Enum.PartType.Block
p.Size = Vector3.new(PartSize, PartSize, PartSize)
local Y = math.noise(X/Smoothness,Z/Smoothness,Seed) * freq
p.CFrame = CFrame.new(X,Y,Z)
if p.Position.Y <= workspace.Terrain1.Position.Y + 5 then
p.Material = Enum.Material.Sand
p.BrickColor = BrickColor.new("Cool yellow")
elseif p.Position.Y >= workspace.Terrain2.Position.Y then
p.Material = Enum.Material.Sand
p.BrickColor = BrickColor.new("White")
elseif p.Position.Y >= workspace.Terrain1.Position.Y then
p.Material = Enum.Material.Grass
p.BrickColor = BrickColor.new("Dark green")
end
end
wait()
end
elseif p.Position.Y >= workspace.Terrain1.Position.Y then
p.Material = Enum.Material.Grass
p.BrickColor = BrickColor.new("Dark green")
if math.random() < 0.1 then
--Clone a tree model, place it at X Y Z coordinates
end
end
You can do what @ThanksRoBama said, or you can use another noise layer:
elseif p.Position.Y >= workspace.Terrain1.Position.Y then
p.Material = Enum.Material.Grass
p.BrickColor = BrickColor.new("Dark green")
local treeDens = math.noise(X/Smoothness,Z/Smoothness,math.floor(Seed / 4) + 3)
if treeDens < 0.00625 and treeDens > -0.00625 then
--generate the tree
end
end
The difference math.random() between math.noise() method is:
math.random() returns a random value, which means if you generated the terrain over and over with same seed, the trees will be positioned at different positions from first terrain generation.
However, math.noise() will return the same number at any time if you didn’t changed values inside of it, which meains if you generated the terrain over and over with same seed, the trees will be positioned at same positions from first terrain generation.
Can you provide the current code that you’re using?
So, I realized that I gave the sample code from my terrain generator, which it uses multiple layers of noise, so the final code should look like this:
local Seed = 1002
local freq = 150
local Smoothness = 200
local treeRate = 20
local Size = 1000
local PartSize = 5
for X = 0,Size,PartSize do -- X Loop
for Z = 0,Size, PartSize do -- Z Loop
local p = Instance.new("Part",workspace.Map)
p.Anchored = true
p.Shape = Enum.PartType.Block
p.Size = Vector3.new(PartSize, PartSize, PartSize)
local Y = math.noise(X/Smoothness,Z/Smoothness,Seed) * freq
p.CFrame = CFrame.new(X,Y,Z)
if p.Position.Y <= workspace.Terrain1.Position.Y + 5 then
p.Material = Enum.Material.Sand
p.BrickColor = BrickColor.new("Cool yellow")
elseif p.Position.Y >= workspace.Terrain2.Position.Y then
p.Material = Enum.Material.Sand
p.BrickColor = BrickColor.new("White")
elseif p.Position.Y >= workspace.Terrain1.Position.Y then
p.Material = Enum.Material.Grass
p.BrickColor = BrickColor.new("Dark green")
local treeDens = (math.noise(X/treeRate,Z/treeRate,math.floor(Seed / 4) + 3) * Smoothness) / Smoothness
if treeDens < 0.00625 and treeDens > -0.00625 then
-- you should change this lines, because this part generates sticks instead of generating tree
local p = Instance.new("Part",workspace.Map)
p.Anchored = true
p.Shape = Enum.PartType.Block
p.Size = Vector3.new(2, PartSize, 2)
p.CFrame = CFrame.new(X,Y + 6,Z)
end
end
end
wait()
end
treeRate is a value which sets tree generation rate. If value is high (ex. 50 or 100) terrain generator generates low amount of trees. If the value is low (ex, 10 or 20) terrain generator generates high amount of trees.
You have to tweak the code for getting better results, good luck!