Script timeout: exhausted allowed execution time

Hello, so i am trying to create mesh after generating heightmap using triangles, but when i draw triangles for bigger maps, it begins to show this error:

Script timeout: exhausted allowed execution time
local wedge = Instance.new("WedgePart");
wedge.Anchored = true;
wedge.TopSurface = Enum.SurfaceType.Smooth;
wedge.BottomSurface = Enum.SurfaceType.Smooth;

local function draw3dTriangle(a, b, c, parent, color)
	local edges = {
		{longest = (c - a), other = (b - a), origin = a},
		{longest = (a - b), other = (c - b), origin = b},
		{longest = (b - c), other = (a - c), origin = c}
	};

	local edge = edges[1];
	for i = 2, #edges do
		if (edges[i].longest.magnitude > edge.longest.magnitude) then
			edge = edges[i];
		end
	end

	local theta = math.acos(edge.longest.unit:Dot(edge.other.unit));
	local w1 = math.cos(theta) * edge.other.magnitude;
	local w2 = edge.longest.magnitude - w1;
	local h = math.sin(theta) * edge.other.magnitude;

	local p1 = edge.origin + edge.other * 0.5;
	local p2 = edge.origin + edge.longest + (edge.other - edge.longest) * 0.5;

	local right = edge.longest:Cross(edge.other).unit;
	local up = right:Cross(edge.longest).unit;
	local back = edge.longest.unit;

	local cf1 = CFrame.new(
		p1.x, p1.y, p1.z,
		-right.x, up.x, back.x,
		-right.y, up.y, back.y,
		-right.z, up.z, back.z
	);

	local cf2 = CFrame.new(
		p2.x, p2.y, p2.z,
		right.x, up.x, -back.x,
		right.y, up.y, -back.y,
		right.z, up.z, -back.z
	);
	
	local wedge1 = wedge:Clone();
	wedge1.Size = Vector3.new(0.01, h, w1);
	wedge1.CFrame = cf1;
	wedge1.Parent = parent;
	wedge1.BrickColor =color;

	local wedge2 = wedge:Clone();
	wedge2.Size = Vector3.new(0.01, h, w2);
	wedge2.CFrame = cf2;
	wedge2.Parent = parent;
	wedge2.BrickColor = color;
end

arr = diamondSquare(arr);
print(arr, tester(arr));
arr = flatter(arr, 2);
local t = 0.3;
local k = 3;

function draw(matrix,y, x)
	local color = BrickColor.new("Bright green");
	local a = matrix[y][x];
	local b = matrix[y + 1][x];
	local c = matrix[y][x + 1];
	local d = matrix[y + 1][x + 1];

	--if(a == c and a == b and a == 0) then
	--	color = BrickColor.new("Pastel Blue")
	--end
	--if(d == c and d == b and d == 0) then
	--	color = BrickColor.new("Pastel Blue")
	--end
	if (b == c and a ~= d ) or (b == c and a == d and b <= a) or (b ~= c and a ~= d)  then
		if(a == b and a == c and a == 0) then
			color = BrickColor.new("Pastel Blue");
		end
		draw3dTriangle(Vector3.new(x, k + t * a, y), Vector3.new(x + 1, k + t * c, y), Vector3.new(x, k +  t * b, y + 1), game.Workspace, color)
		
		color = BrickColor.new("Bright green");
		if(d == b and d == c and d == 0) then
			color = BrickColor.new("Pastel Blue");
		end
		draw3dTriangle(Vector3.new(x + 1, k + t * d, y + 1), Vector3.new(x + 1, k + t * c, y), Vector3.new(x, k +  t * b, y + 1), game.Workspace, color)
	else
		
		if(a == b and b == d and b == 0) then
			color = BrickColor.new("Pastel Blue");
		end
		draw3dTriangle(Vector3.new(x, k + t * b, y + 1), Vector3.new(x, k + t * a, y), Vector3.new(x + 1, k +  t * d, y + 1), game.Workspace, color)
		
		color = BrickColor.new("Bright green");
		if(d == c and c == a and c == 0) then
			color = BrickColor.new("Pastel Blue");
		end
		draw3dTriangle(Vector3.new(x + 1, k +  t * c, y), Vector3.new(x, k +  t * a, y), Vector3.new(x + 1, k +  t * d, y + 1), game.Workspace, color)
	end
end


for y = 1, n - 1 do
	for x = 1, n - 1 do
		draw(arr, y, x);
		--print(arr[y][x], arr[y][x + 1], arr[y + 1][x], arr[y + 1][x + 1]);
	end
end

I guess it’s because there is too much For_do loop without Wait time.
This error happen when scripts do too much things, too fast.

for y = 1, n - 1 do
	for x = 1, n - 1 do
		draw(arr, y, x);
		--print(arr[y][x], arr[y][x + 1], arr[y + 1][x], arr[y + 1][x + 1]);
        task.wait()
	end
    task.wait()
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.