Script Exhaust when building an island

Hello, I am working on an island code, and the script I use always exhausts after I join. I’ve tried to fix it with wait() or task.wait() but they make the generation so slow it could take 15 minutes or more to generate the island. Does anyone know a way around this?

Code:

local X, Z = 300, 300

local tile_size = 2

local wedge = Instance.new("WedgePart");
wedge.Name = "Part"
wedge.Anchored = true;
wedge.TopSurface = Enum.SurfaceType.Smooth;
wedge.BottomSurface = Enum.SurfaceType.Smooth;

local function draw3dTriangle(a, b, c)
	local ab, ac, bc = b - a, c - a, c - b;
	local abd, acd, bcd = ab:Dot(ab), ac:Dot(ac), bc:Dot(bc);

	if (abd > acd and abd > bcd) then
		c, a = a, c;
	elseif (acd > bcd and acd > abd) then
		a, b = b, a;
	end

	ab, ac, bc = b - a, c - a, c - b;

	local right = ac:Cross(ab).unit;
	local up = bc:Cross(right).unit;
	local back = bc.unit;

	local height = math.abs(ab:Dot(up));
	local w1 = wedge:Clone();
	w1.Size = Vector3.new(0, height, math.abs(ab:Dot(back)));
	w1.CFrame = CFrame.fromMatrix((a + b)/2, right, up, back);
	w1.Parent = game.Workspace.Island.Terrain;

	local w2 = wedge:Clone();
	w2.Size = Vector3.new(0, height, math.abs(ac:Dot(back)));
	w2.CFrame = CFrame.fromMatrix((a + c)/2, -right, up, -back);
	w2.Parent = game.Workspace.Island.Terrain;
	
	local w2height = w2.Position.Y
	local w1height = w1.Position.Y

-- Where the problem seems to happen when I added this

	if w2height <= -4 and w1height <= -4 then
		w2.BrickColor = BrickColor.new("Cool yellow") 
		w2.Material = Enum.Material.Sand
		w1.BrickColor = BrickColor.new("Cool yellow") 
		w1.Material = Enum.Material.Sand
	else
		w2.BrickColor = BrickColor.new("Forest green") 
		w2.Material = Enum.Material.Grass
		w1.BrickColor = BrickColor.new("Forest green") 
		w1.Material = Enum.Material.Grass
	end
-- end of problem

	return w1, w2;
end

local positionGrid = {}

for x = 0, X do
	positionGrid[x] = {}

	for z = 0, Z do
		positionGrid[x][z] = Vector3.new(x*tile_size, math.noise(x/100, z/100) * 35, z*tile_size)
	end
end

for x = 0, X-1 do
	for z = 0, Z-1 do
		local a = positionGrid[x][z]
		local b = positionGrid[x+1][z]
		local c = positionGrid[x][z+1]
		local d = positionGrid[x+1][z+1]

		draw3dTriangle(a, b, c)
		draw3dTriangle(b, c, d)
		
		
	end
end

Any help is greatly appreciated, and if anyone could help with runoff islands on this that would be great!

2 Likes

You need waits in there somewhere, otherwise it overwhelms the game engine. Try seeing if you can get an acceptable value to wait that meets your load time expectations and not overwhelming the game engine.

task. wait() yields for single frame, so at 60FPS that would represent a delay of roughly ~0.015 seconds, which is pretty small anyway. How many times are you looping in total?

1 Like

Probably up to 180 thousand times.

1 Like

Which it crashes around 40-60 thousand loops through

1 Like

I fixed the issue by just removing the else, and putting that at the top.

1 Like