How to generate a large amount of instance without lagging

The maze algorithm I made generates large amounts of instances once the maze is fully created. It iterates through every node and spawn the wall based on the information. Is there a way to generate large amount of instances without crashing? I also want to make the speed of the maze generation fast enough while not crashing the server. I didn’t find any dev forum posts that could help me. Is it impossible? Thanks for helping!

for i, Node in ipairs(MazeGenerator.Nodes) do
	Node:SpawnWalls() 
end
:SpawnWalls() method
function NodeClass:SpawnWalls()
	local MazeService = Knit.GetService("MazeService")
	local StartingVector = MazeService:GetConfigurations().StartingVector
	local CellSize = MazeService:GetConfigurations().CellSize
	local Height = MazeService:GetConfigurations().Height
	local Thickness = MazeService:GetConfigurations().Thickness
	local Material = MazeService:GetConfigurations().Material
	local Model = Instance.new("Model")

	Model.Name = self.X .. ", " .. self.Y

	local MiddlePoint = Vector3.new(StartingVector.X + CellSize * self.X, StartingVector.Y - (Height / -2), StartingVector.Z + CellSize * self.Y)
	self.Position = MiddlePoint
	
	for Index,_ in pairs(self.Walls) do
		local Wall = Instance.new("Part")
		Wall.Anchored = true
		Wall.Material = Material
		
		if Index == "Up" then
			Wall.Position = Vector3.new(MiddlePoint.X - CellSize / 2, MiddlePoint.Y, MiddlePoint.Z)
			Wall.Size = Vector3.new(CellSize,Height,Thickness)
		end
		
		if Index == "Down" then
			Wall.Position = Vector3.new(MiddlePoint.X - CellSize / 2, MiddlePoint.Y, MiddlePoint.Z - CellSize)
			Wall.Size = Vector3.new(CellSize,Height,Thickness)
		end
		
		if Index == "Left" then
			Wall.Position = Vector3.new(MiddlePoint.X - CellSize, MiddlePoint.Y, MiddlePoint.Z - CellSize / 2)
			Wall.Size = Vector3.new(Thickness,Height,CellSize)
		end
		
		if Index == "Right" then
			Wall.Position = Vector3.new(MiddlePoint.X, MiddlePoint.Y, MiddlePoint.Z - CellSize / 2)
			Wall.Size = Vector3.new(Thickness,Height,CellSize)
		end
		Wall.Name = Index
		Wall.CanTouch = false
		Wall.CanQuery = false
		Wall.Parent = Model
	end
	
	local TopWall = Instance.new("Part")
	TopWall.Transparency = 1
	TopWall.Material = Material
	TopWall.Anchored = true
	TopWall.Size = Vector3.new(CellSize, Thickness, CellSize)
	TopWall.Position = Vector3.new(MiddlePoint.X - CellSize / 2, MiddlePoint.Y + Height / 2, MiddlePoint.Z - CellSize / 2)
	TopWall.Name = "Top"
	TopWall.CanTouch = false
	TopWall.CanQuery = false
	
	local BottomWall = Instance.new("Part")
	BottomWall.Material = Material
	BottomWall.Anchored = true
	BottomWall.Size = Vector3.new(CellSize, Thickness, CellSize)
	BottomWall.Position = Vector3.new(MiddlePoint.X - CellSize / 2, MiddlePoint.Y - Height / 2, MiddlePoint.Z - CellSize / 2)
	BottomWall.Name = "Bottom"
	TopWall.CanTouch = false
	TopWall.CanQuery = false
	
	TopWall.Parent = Model
	BottomWall.Parent = Model
	Model.Parent = MazeModel
end```
1 Like

You say ‘a large amount’ but how many is a large amount?

If you mean 100 items it shouldn’t be a huge issue.

If you are looping through 10,000 of those items with no wait then you’ll probably have an issue because the script is holding up other calculations during that loop. You can try putting a task.wait() inside the loop to see if that helps.

It depends on how big my argument is, i tested it with a maze size of 150 and it only caused slight lag, however when generating bigger mazes lag starts to occur and task.wait() yields the code and makes the generation process slower and I want to avoid that.

Are all the Part sizes variable, or will they have set sizes?
If they’re all set sizes you may be able to have a set of the pre-sized Parts in ServerStorage and clone them out to save the calculations for creating a new Part each time. You’d just have to calculate the cloned Part’s Position and Orientation.

yes, part size is represented variable, I’ll try to implement cloning blocks to server storage to workspace and see how much of a performance boost it will give.

As far as I’m aware, there isn’t really a way of spawning a lot of parts that doesn’t have its compromises. If you want to avoid slowing down the generation of the maze while keeping good performance, you could try checking the player’s position every so often (say, every RunService.Heartbeat) and only creating the parts that are near the player. However, this does put a sustained load on the server which might be an issue if your game needs to have large server sizes or if the players move too fast

Thanks for telling me I would use your method of cloning from server storage & yield every once in a while, so it won’t lag that much while keeping up with the generation speed.

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