Reducing script lag on World generator

I have a world generator that generates biomes, and caves using roblox terrain, but this script runs at 50-55% activity and a rate of 5 per second when generating a grid of 6x6 chunks, each having a grid of 30x30, ingame it results in 500-600 ms when generating terrain. I never got close to this much script activity before, and I think it will lag my game when generating new chunks. I already tried increasing the number of chunks and reducing size accordingly, giving yields on caves, putting yields on the forx and forz function, but either it doesnt slow down as much as i wanted or it does but the generation takes way too long to complete.

It seems like I found a way to fix this issue, but I am not 100% sure. If you stop generating the terrain and trees/objects, and instead send a signal for the client to generate them at specific locations, maybe the game wont lag as much serverside to generate. It resulted in a 8% activity instead of 50-55%. The problem is that I’ll have to redo a lot of code and there will be fps loss when generating, also enemies will become a problem to script. (Maybe make the nearest player control their script and replicate it, not so much of a problem for cheaters cuz this game would be coop).

you can do what Minecraft does and make chunks appear when you get close, prevents lag.

I already made it, the problem is when they are being generated though. But I guess it might not be a huge problem, because I might let damage calculations and other mechanics like placing/mining client-side, because it wont lag and I wont care for cheaters, because this will be a coop game. I’ll later make a non infinite pvp arena on a different place for them to teleport and there server scripts will be used.

You should try making your code more efficient. While this is a vague statement you didn’t really provide us with a sample so there’s not really much we can recommend. With the coming release of Parallel Lua you might be able to run the code for your world generator on a separate hardware thread.

Sadly, creating new instances of Part and replication is slow on Roblox. I also have similar problems with world generation. I had to reduce the speed of the generator in order not to lag the server. One of my ideas was the same as you wanted to do - move the load from the server to the client and generate the map on the client, but this is very complex because server has to know where an enemy is (to make checks that a player is close to it for example).

oh alright, well before hand you can create a gui that slowly loads the chunks and keep the player far away, then teleport them to the starting area.

The chunks are also loaded as the player walks through the terrain, thats the problem. If it was just the starting area it would be good to have just a loading gui.