My tree generation inside my terrain generation script is being quite weird, I have no idea what’s causing this and it’s probably something obvious lol. Anybody know how to fix this?
Tree gen code:
for x = 1, CHUNK_SX do
for z = 1, CHUNK_SX do
if generator.NextInteger(generator, 0, 300) > 299 then
for y = CHUNK_SY, 1, -1 do
if materials[x][y][z] == Enum.Material.Air then
continue
else
if grid[x][y][z] ~= 0 then
worldPos = Vector3.new(x, y, z) + Vector3.new(chunkX, 0, chunkZ) * Vector3.new(CHUNK_SX, 0, CHUNK_SZ)
local tree = game.ReplicatedStorage.Objects.Tree:Clone()
tree.PrimaryPart.CFrame = CFrame.new(worldPos * Vector3.new(RESOLUTION, RESOLUTION, RESOLUTION)) + Vector3.new(0, (tree.trunk.Size / 2), 0)
tree.Parent = workspace
break
end
end
end
end
end
end
full script (if you need to know what some of the variables are):
local CHUNK_SX = 16
local CHUNK_SY = 128
local CHUNK_SZ = 16
local RESOLUTION = 4
local SCALE = 30 --scales the terrain, lower = bumpy/holes, higher = smoother terrain
local worldPos = Vector3.new(0, 0, 0)
local chunkOriginInStuds = Vector3.new(0, 0, 0)
local chunkSizeInStuds = Vector3.new(0, 0, 0)
local function pair2u(a, b)
return if a >= b then (a*a) + a + b else (b*b) + a
end
-- given two positive or negative numbers
local function pair2(a, b)
a = if a > 0 then 2*a else 2*-a + 1
b = if b > 0 then 2*b else 2*-b + 1
return pair2u(a, b)
end
--world seed
local generator
local grid = {}
local materials = {}
local function generateChunk(chunkX, chunkZ)
local seed = pair2(chunkX, chunkZ)
generator = Random.new(seed)
for x = 1, CHUNK_SX do
grid[x] = {}
for y = 1, CHUNK_SY do
grid[x][y] = {}
for z = 1, CHUNK_SZ do
chunkOriginInStuds = Vector3.new(chunkX * CHUNK_SX * RESOLUTION, y, chunkZ * CHUNK_SZ * RESOLUTION)
chunkSizeInStuds = Vector3.new(CHUNK_SX, CHUNK_SY, CHUNK_SZ) * Vector3.new(RESOLUTION, RESOLUTION, RESOLUTION)
worldPos = Vector3.new(x, y, z) + Vector3.new(chunkX, 0, chunkZ) * Vector3.new(CHUNK_SX, 1, CHUNK_SZ)
local perlinX = worldPos.X
local perlinZ = worldPos.Z
local diff = math.noise(perlinX/SCALE, y/SCALE, perlinZ/SCALE) - ((y - 20) / 5)
grid[x][y][z] = math.clamp(diff / 0.2, 0, 1)
end
end
end
for x = 1, CHUNK_SX do
materials[x] = {}
for y = 1, CHUNK_SY do
materials[x][y] = {}
for z = 1, CHUNK_SZ do
materials[x][y][z] = Enum.Material.Grass
end
end
end
local region = Region3.new(chunkOriginInStuds, chunkOriginInStuds + chunkSizeInStuds)
workspace.Terrain:WriteVoxels(region, RESOLUTION, materials, grid)
for x = 1, CHUNK_SX do
for z = 1, CHUNK_SX do
if generator.NextInteger(generator, 0, 300) > 299 then
for y = CHUNK_SY, 1, -1 do
if materials[x][y][z] == Enum.Material.Air then
continue
else
if grid[x][y][z] ~= 0 then
worldPos = Vector3.new(x, y, z) + Vector3.new(chunkX, 0, chunkZ) * Vector3.new(CHUNK_SX, 0, CHUNK_SZ)
local tree = game.ReplicatedStorage.Objects.Tree:Clone()
tree.PrimaryPart.CFrame = CFrame.new(worldPos * Vector3.new(RESOLUTION, RESOLUTION, RESOLUTION)) + Vector3.new(0, (tree.trunk.Size / 2), 0)
tree.Parent = workspace
break
end
end
end
end
end
end
end
for chunkX = 0, 16 do
for chunkZ = 0, 16 do
task.wait()
generateChunk(chunkX, chunkZ)
end
end
Image of what’s happening:
If you can help it would be greatly appreciated! It should be noted I’m still a beginner at scripting and I made this from purely coding knowledge and no tutorials, so if my code isn’t very readable or there’s some unnecessary lines of code then that’s why.