How to stop script timeout from huge terrain

I am scripting a game that is constantly loading in & clearing large maps of terrain. loading isn’t an issue, as I have the loading split in 2. currently, when using workspace.Terrain:Clear(), there is a whole script exhaustion. I’m not that experienced in managing terrain by script, and the only method I could think of is load by chunks, however I have absolutely no idea how I would script that, not even to mention there aren’t any YT videos on this either. any help would be great with this

If you have a large area of Terrain loaded, and you run Terrain:Clear() on it, it will (as should be expected) delete all of the terrain you have loaded. This is horribly inefficient if you are then re-creating most of that seconds later. The better way to go about loading and unloading is–as you said–in chunks. I’d recommend using Terrain:FillBlock(), and writing your own logic for chunks. It depends how you’re generating the terrain, but it should be no more than several nested for-loops. To really be “careful”, you can have an array specifying what chunks are loaded, and at a certain interval you can iteratre through the array and delete any chunks that are a certain distance from the player.

I think I tried a method similar to what you are mentioning. I had blocks scattered across the map and used for-loops and converted all of them to air via Terrain:FillBlock(), unfortunately I got the same end result (timeout). Loading & Clearing the terrain maps kind of work like a minigame system, except of models it’s terrain. would I use parts, or just Terrain.MaxExtents (original :PasteRegion position) for the :FillBlock line?