Why plugin drops VISIBLE productivity, but works like 30-40 FPS?

Hello. I’m trying make my own plugin. for now, I’m trying make Grow/Erode tools like in defaut terrain editor, and they work pretty well, but using them drops visual productivity up to 1-5 FPS, but in the same time, plugin works with productivity of 30-40 FPS. Can someone tell me, what can be main reason of this productivity diffirence?
image
(Plugin workspeed)
image
(FPS and Render)

local CurrentTime = os.clock()
local RegionStart = Vector3.new(math.floor((BrushPart.Position.X - BrushPart.Size.X / 2) / 4) * 4, math.floor((BrushPart.Position.Y - BrushPart.Size.Y / 2) / 4) * 4, math.floor((BrushPart.Position.Z - BrushPart.Size.Z / 2) / 4) * 4)
local RegionEnd = Vector3.new(math.ceil((BrushPart.Position.X + BrushPart.Size.X / 2) / 4) * 4, math.ceil((BrushPart.Position.Y + BrushPart.Size.Y / 2) / 4) * 4, math.ceil((BrushPart.Position.Z + BrushPart.Size.Z / 2) / 4) * 4)
print(tostring(RegionStart) .. ", " .. tostring(RegionEnd))
local Voxels = Region3.new(RegionStart, RegionEnd)
local Materials, Occupancies = workspace.Terrain:ReadVoxels(Voxels, 4)
print(#Occupancies, #Occupancies[1], #Occupancies[1][1])
for X = RegionStart.X + 2, RegionEnd.X - 2, 4 do
	for Y = RegionStart.Y + 2, RegionEnd.Y - 2, 4 do
		for Z = RegionStart.Z + 2, RegionEnd.Z - 2, 4 do
			local Direction = (Vector3.new(X, Y, Z) - BrushPart.Position).Unit
			local DirectionMultiplication = if Direction.X >= Direction.Y and Direction.X >= Direction.Z then 2 / Direction.X elseif Direction.Y >= Direction.X and Direction.Y >= Direction.Z then 2 / Direction.Y else 2 / Direction.Z
			Direction = Direction * DirectionMultiplication
			if (Vector3.new(X, Y, Z) - BrushPart.Position - Direction).Magnitude <= BrushPart.Size.X then
				print("Voxel is touched")
				print((X + 2 - RegionStart.X), (X + 2 - RegionStart.X) / 4)
				print((Y + 2 - RegionStart.Y), (Y + 2 - RegionStart.Y) / 4)
				print((Z + 2 - RegionStart.Z), (Z + 2 - RegionStart.Z) / 4)
				if Occupancies[(X + 2 - RegionStart.X) / 4][(Y + 2 - RegionStart.Y) / 4][(Z + 2 - RegionStart.Z) / 4] < 1 then
					if Materials[(X + 2 - RegionStart.X) / 4][(Y + 2 - RegionStart.Y) / 4][(Z + 2 - RegionStart.Z) / 4] == Enum.Material.Air then
						Materials[(X + 2 - RegionStart.X) / 4][(Y + 2 - RegionStart.Y) / 4][(Z + 2 - RegionStart.Z) / 4] = Enum.Material[ToolSettings[Tool]["Materials"]["Materials"][2]]
					end
					Occupancies[(X + 2 - RegionStart.X) / 4][(Y + 2 - RegionStart.Y) / 4][(Z + 2 - RegionStart.Z) / 4] = Occupancies[(X + 2 - RegionStart.X) / 4][(Y + 2 - RegionStart.Y) / 4][(Z + 2 - RegionStart.Z) / 4] + ToolSettings[Tool]["Brush"]["Power"][2] / 250
				end
			else
				print("Voxel is too far from brush")
				print((X + 2 - RegionStart.X), (X + 2 - RegionStart.X) / 4)
				print((Y + 2 - RegionStart.Y), (Y + 2 - RegionStart.Y) / 4)
				print((Z + 2 - RegionStart.Z), (Z + 2 - RegionStart.Z) / 4)
			end
		end
	end
end
workspace.Terrain:WriteVoxels(Voxels, 4, Materials, Occupancies)
warn(tostring(os.clock() - CurrentTime) .. " is time used")

Also, this code runs every time mouse or camera moved

Too many nested for loops which become exponentially more expensive the more times you iterate or the more nested loops you have.

1000 iterations with all the printing statements and all the value manipulations and all the if blocks per camera movement / mouse movement will cause LAG.

Can’t you separate the loops? Or at least put all the code into one loop?

Also, too many print statements being called is probably why the dramatic FPS decrease occurs with printing.

2 Likes

Guy, that’s strange for me, but converting prints to comments is REALLY REDUCE LAG. THX.
(Also, I can’t remove all this for loops, bc I need check every voxel in 3D array)