Terrain:CopyRegion exhausting script resources

  1. What do you want to achieve?
    I want to copy and paste the entirety of the terrain at the end of a game.

  2. What is the issue? Include screenshots / videos if possible!
    I get “script timeout: exhausted allowed execution time” when I try to assign a variable:

terrainCopy = workspace.Terrain:CopyRegion(workspace.Terrain.MaxExtents)

This works completely fine without issues in studio. However, when I play the game on Roblox I get “Script timeout: exhausted allowed execution time” and it usually points to a random line in the code which is odd.

  1. What solutions have you tried so far?
    I’ve already looked around the code and tried different things. The only solution is to remove the shown line of code. It’s worth mentioning I’ve tried putting this line in a coroutine to see if that would help the issue and the problem still persists

Terrain:CopyRegion causing script timeouts is likely due to the heavy computational load required to copy a large region of terrain. This can easily exceed the execution time limit for a single script especially when running outside of studio.

You have a couple options here:

  1. Instead of copying the entire region in one go, break it down into smaller parts.
  2. Use Task Scheduling: You can also use the task.defer or task.wait to yield the execution and avoid hitting the timeout limit.
1 Like

Thank you for posting your solution, I decided to go with copying the terrain as an object in studio and parenting it to server storage and just pasting it to the terrain during runtime. I still had to divide the terrain in 2 but this takes out the extra lines of code that otherwise would of caused further issues. Thank you again!

Here’s the link to the forum post where I found this solution