Making loading system faster

I am currently making a voxel game, and I need help making the loading faster, I load things by generate part in a model that is parented to nil, and then parent that model to workspace. Here is the loading thing


I want to make the loading faster, how?
:

1 Like

Can you link the code so we can see?

#code-review :slight_smile:

1 Like

Here.

local Players = game:GetService("Players")
local BLOCK_SIZE = 3
local CHUNK_SCALE = 16
local RENDER_DISTANCE = 80
local seed = math.random(-10e6,10e6)
local NOISE_SCALE = 30

local chunks = {}

local function chunkExists(chunkX, chunkZ)
	if not chunks[chunkX] then
		chunks[chunkX] = {}
		if not chunks[chunkZ] then
			chunks[chunkZ] = {}
			return chunks[chunkX],chunks[chunkZ]
		end
	end
end

local function mountLayer(x, heightY, z, material, parent)
	local cframe = CFrame.new(x * BLOCK_SIZE, heightY * BLOCK_SIZE, z * BLOCK_SIZE)
	local size = Vector3.new(BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)

	local block = workspace.Part:Clone()
	block.CFrame = cframe
	block.Size = size
	block.Material = material
	block.Parent = parent
end

function makeChunk(chunkX, chunkZ, chunkY)
	local chunk_object = game:GetService("ReplicatedStorage").chunk_object:Clone()
	chunk_object.Parent = nil

	chunks[chunkX][chunkZ] = true

	for x = 0, CHUNK_SCALE do
		for z = 0, CHUNK_SCALE do
			for y = 0, CHUNK_SCALE do
				local cx = (chunkX * CHUNK_SCALE) + x
				local cz = (chunkZ * CHUNK_SCALE) + z

				local noise_X = math.noise(y/NOISE_SCALE,cz/NOISE_SCALE,seed) * 5
				local noise_Y = math.noise(cx/NOISE_SCALE,cz/NOISE_SCALE,seed) * 5
				local noise_Z = math.noise(cx/NOISE_SCALE,y/NOISE_SCALE,seed) * 5

				local density = noise_X + noise_Y + noise_Z + y

				if density > 8 and density < 10 then
					mountLayer(cx, y, cz, Enum.Material.Grass, chunk_object)					
				end			
			end
		end
	end

	task.wait()
	chunk_object.Parent = workspace.terrain
end

function destroy_chunks(location)
	for i, chunk in pairs(workspace.terrain:GetChildren()) do
		if (chunk:GetPivot().Position - location).Magnitude > 50 then
			chunk:Destroy()
		end
	end
end

function checkSurroundings(location)
	local chunkX, chunkZ = math.floor(location.X / BLOCK_SIZE / CHUNK_SCALE), math.floor(location.Z / BLOCK_SIZE / CHUNK_SCALE)
	local range = math.max(1, RENDER_DISTANCE / CHUNK_SCALE)
	for x = -range, range do
		for z = -range, range do
			local cx = chunkX + x
			local cz = chunkZ + z

			if not chunkExists(cx, cz) then
				makeChunk(cx, cz)
			end			

			destroy_chunks(location)
		end
	end
end

while true do
	for _, player in pairs(Players:GetPlayers()) do
		if player.Character then
			local humanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")
			if humanoidRootPart then
				checkSurroundings(humanoidRootPart.Position)
			end
		end
	end
	wait(0.5)
end
while true do
	for _, player in pairs(Players:GetPlayers()) do
		if player.Character then
			local humanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")
			if humanoidRootPart then
				checkSurroundings(humanoidRootPart.Position)
			end
		end
	end
	wait(0.5)
end

maybe instead of while true do, use renderservice.renderstepped instead? or even reduce the wait from 0.5 to just wait()

I don’t have a solid solution but you should look into Parallel Lua that’s used for each chunk generation. It’ll speed things up a lot.

More specifics + a demo here:

1 Like