Terrain Generation Issues

This is my current mess of a script to generate some small terrain chunk with plants and tree’s on it.

Currently it can only generate small area’s of land, and even when it does, it does it very slowly and lags the entire game. What I eventually want it to do is generate terrain around the player in chunks, however not only do I have no idea how to make chunks, but I can’t control the positioning of where the terrain is even generated, as its always generated around 0,0,0.

So in Laymen’s terms, I have a Laggy terrain generator that only generates terrain at the center of the map and have no idea how to branch off this to get a chunk system to generate terrain smoothly around the player without my pc catching fire. Yes I’ve looked around and done research on it, but I can’t find anywhere where it states how exactly in scripting terms, it is done. And even when I get a module or script for it, I can’t understand a single line of code because of how weirdly thrown together the variables are.

Hopefully this sums up my stupidity enough and I’d gladly like some feedback on how to fix this up.

1. Do please NOT post images of code, but instead use the code-format style, using 3 x backtick symbol for start and end:

 ```lua
    (paste your code within these triple-backticks lines)
 ```

2.
Instead of doing one Terrain:FillBlock per-each-single-position, you should consider using the Terrain:WriteVoxels, as it can take a 3-dimensional-array of both the materials and “fill-level” (occupancy) for those “each-single-position”.

It will most likely be run-time faster to build a 3-dimensional-array of some chunk-size (lets say 32 x 8 x 32), and WriteVoxels for that, then build the next chunk, WriteVoxels, etc.

Take a look at the ‘Reading and writing voxels’ article. - There is even, in the ‘Examples’ chapter, a section for ‘Procedural terrain generation’ (which uses Terrain:FillBlock), based on how the player moves around in the world.

3.
Consider moving the partsize assignment out before doing the for-loops, as your code never modifies blocksize (it stays constant 1.5), so there is no need to create 400 * 400 * 2000 Vector3s…

4.

Tree.Parent = workspace

Try to avoid placing “everything” at the root level of Workspace. Make some Model or Folder instance, and set the cloned trees’ parent to that model or folder, so the root of Workspace contains fewer objects.

5.

mapysize = 2000
-- ...
for y = 0, mapysize do
   -- ...
   density = xnoise + znoise + ynoise + y
   if density < 20 and density > 17 then

Do think about how many iterations that is performed, which does NOT make that if-statement true.

Maybe you can reduce the mapysize value by a huge amount, thereby having fewer iterations, thereby optimizing your terrain generation.

I’ve already gotten a much better script that’s less resource intensive however I’ll go check out the WriteVoxels function because that does seem very usefull later down the line. (I found out the error after writing this article, so its all patched up now so my pc isin’t on fire. But the function you mentioned could make it a lot more seemless if done right). Thanks for the help, I’ll post again if I find anything new.