Performance problems

Greetings there so I have script which makes infinite terrain but there’s an problem, then I play the game, the game performance is poor, and I have no idea how to increase performance.

  1. What do you want to achieve?
    That the game would run at normal speed
  2. What is the issue?
    The huge lag in the game due of infinite terrian generaitor script
    The video to prove:

The script

local re = game:WaitForChild("ReplicatedStorage")
local ree = re:WaitForChild("ITSS")

ree.OnServerEvent:Connect(function(player, x, z)
	local size = script:GetAttribute("Size") or 64
	local load = script:GetAttribute("Load") or 16
	local unload = script:GetAttribute("Unload") or 32
	local events = {["Initialize"] = {}, ["Deinitialize"] = {}, ["Load"] = {}, ["Unload"] = {}}
	local modules = {}
	local loaded = {}
	local cameraX = math.huge
	local cameraZ = math.huge
	local focusX = math.floor(cameraX / size)
	local focusZ = math.floor(cameraZ / size)
	local amount = (load * 2 + 1) ^ 2
	local distance = size ^ 2
	load = load ^ 2 + 1
	unload = unload ^ 2 + 1

	local function LoadChunk(chunkX, chunkZ)
		if loaded[chunkX] == nil then loaded[chunkX] = {} end
		if loaded[chunkX][chunkZ] ~= nil then return end
		loaded[chunkX][chunkZ] = true
		for i, callback in events.Load do callback(script, modules, chunkX, chunkZ) end
		task.wait()
	end

	local function UnloadChunk(chunkX, chunkZ)
		loaded[chunkX][chunkZ] = nil
		if next(loaded[chunkX]) == nil then loaded[chunkX] = nil end
		for i, callback in events.Unload do callback(script, modules, chunkX, chunkZ) end
		task.wait()
	end

	for i, descendant in script:GetDescendants() do
		if descendant.ClassName ~= "ModuleScript" then continue end
		local module = require(descendant)
		modules[descendant] = module
		if type(module) ~= "table" then continue end
		for name, callbacks in events do
			if type(module[name]) == "function" then table.insert(callbacks, module[name]) end
		end
	end

	for i, callback in events.Initialize do callback(script, modules) end

	script.AttributeChanged:Connect(function(attribute)
		if attribute == "Size" then
			size = script:GetAttribute("Size") or 64
			distance = size ^ 2
		end
		if attribute == "Load" then
			load = script:GetAttribute("Load") or 16
			amount = (load * 2 + 1) ^ 2
			load = load ^ 2 + 1
		end
		if attribute == "Unload" then
			unload = script:GetAttribute("Unload") or 32
			unload = unload ^ 2 + 1
		end
	end)
	focusX, focusZ = math.floor(x / size), math.floor(z / size)
	if (cameraX - x) ^ 2 + (cameraZ - z) ^ 2 <= distance then return end
	cameraX, cameraZ = x, z
	local chunkX, chunkZ, directionX, directionZ, count, length = focusX, focusZ, 1, 0, 0, 1
	for i = 1, amount do
		if (chunkX - focusX) ^ 2 + (chunkZ - focusZ) ^ 2 <= load then LoadChunk(chunkX, chunkZ) end
		chunkX += directionX chunkZ += directionZ count += 1
		if count == length then count = 0 directionX, directionZ = -directionZ, directionX if directionZ == 0 then length += 1 end end
	end
	if unload < load then return end
	for chunkX, data in loaded do
		for chunkZ, value in data do
			if (chunkX - focusX) ^ 2 + (chunkZ - focusZ) ^ 2 <= unload then continue end
			UnloadChunk(chunkX, chunkZ)
		end
	end
end)

P.S my still learning scripting soo dont mind explaining what it is and what it does