Script timeout: exhausted allowed execution time while moving trees

I am trying to spawn trees around a map but when I press play, The game won’t generate trees or ground. (I’m using Diegnified’s Perlin Noise Module for the ground.)

I have looked at using coroutine but I can’t figure out how to use that. I have also tried to look at other devforum posts but it still won’t work.

Scripts
local PerlinNoise = require(game.ServerStorage.PerlinNoise)

local size = 250
local amplitude = math.random(30, 35)
local octaves = 1
local height = 10
local partSize = Vector3.new(2,5,2)

for x = 1,size,1 do
   for y = 1,size,1 do
      local currentpart = Instance.new("Part")
      currentpart.Position = Vector3.new(x*2,PerlinNoise.new({x,y},amplitude,octaves,0.5)*height,y*2)
      currentpart.Size = partSize
      currentpart.Anchored = true
	  currentpart.CastShadow = false
	  currentpart.BrickColor = BrickColor.new("Camo")
	  currentpart.Material = "Grass"
      currentpart.Parent = workspace.MapTerrain.Land
	  if math.random(1, 4) == 2 then
		local currenttree = game.ServerStorage.Tree:Clone()
		currenttree:MoveTo(currentpart.Position + Vector3.new(1, 0, 1))
		currenttree.Parent = workspace.MapTerrain.Trees
	  end
   end
end

Tree model: tree.rbxm (6.9 KB)

Diegnified’s Perlin Noise Module: Perlin Noise library Module! (4D and octaves!)

Please help.

This error reminds me of a while loop without wait(). In this case, it looks like this for loop is trying to do a lot of things very quickly, causing a timeout. See if adding a wait() at the end would help ease the overflow.

I just tried using wait() but no terrain is generated and there are no errors. I tried moving it a line down but it still wouldn’t work. Moved it down another line and I get a VERY low fps and a crash. (still no generation)

I got studio crashing on mine. I learned that you’re trying to put WAY too many trees at once.

I adjusted the script to ease off the amount of trees spawning:

local PerlinNoise = require(game.ServerStorage.PerlinNoise)

local size = 250
local amplitude = math.random(30, 35)
local octaves = 1
local height = 10
local partSize = Vector3.new(2,5,2)

for x = 1,size,1 do
	for y = 1,size,1 do
		local currentpart = Instance.new("Part")
		currentpart.Position = Vector3.new(x*2,PerlinNoise.new({x,y},amplitude,octaves,0.5)*height,y*2)
		currentpart.Size = partSize
		currentpart.Anchored = true
		currentpart.CastShadow = false
		currentpart.BrickColor = BrickColor.new("Camo")
		currentpart.Material = "Grass"
		currentpart.Parent = workspace.MapTerrain.Land
		if math.random(1, 400) == 2 then
			local currenttree = game.ServerStorage.Tree:Clone()
			currenttree.Parent = workspace.MapTerrain.Trees
			currenttree:MoveTo(currentpart.Position + Vector3.new(1, 0, 1))
			wait()
			print("tree")
		end
	end
	wait()
end

I suspect that you think MoveTo just sets the position of that - what I assume is a - Model you have cloned.

Though MoveTo does much more, so it would probably be better (since this is auto-generating landscape code) to either use SetPrimaryPartCFrame or TranslateBy - assuming you have remembered to specify the PrimaryPart for the Model.

The issue is a mix of the trees not being moved (all in the same CFrame) and too many trees being generated. It’s working now!

It actually looks pretty good lol

1 Like