How to make optimization system like this one

How can i make an optimization system like this video?

Explanation of the optimization: Some of the parts in a range from approximately 15-35 studs from the player get optimized by scaling then and merging some of the parts, and when the parts are closely enough to the player, they go to their original form, and after they are far enough to the player they get scaled or merged again.

Parts should get both scaled and merged (Some scaled, some scaled and merged)

I have already went to several parts and even asked ai, but none of the scripts worked to achieve this optimization.

Script below contains the code, which obviously, does not work and i don’t know why.

local Players = game:GetService("Players")
local workpace = game:GetService("Workspace")

local SCALE_FACTOR = 1.5
local RANGE = 30
local FOLDER_NAME = "Parts" -- Replace with the actual folder name

local function optimizePartSize(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
	local folder = workpace:FindFirstChild(FOLDER_NAME)

	if not folder then
		warn("Folder not found: " .. FOLDER_NAME)
		return
	end

	while true do
		for _, part in folder:GetChildren() do
			if part:IsA("BasePart") then
				local distance = (humanoidRootPart.Position - part.Position).Magnitude
				if distance == RANGE then
					part.Size = part.Size * SCALE_FACTOR
				else
					part.Size = part.Size / SCALE_FACTOR
				end
			end
		end
		task.wait(1) -- Check every second
	end
end

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		optimizePartSize(player)
	end)
end)

for _, player in Players:GetPlayers() do
	player.CharacterAdded:Connect(function()
		optimizePartSize(player)
	end)
end