Make loading faster

Hello, I am currently making a voxel game, and the problem is the loading is too slow. The loading method I used is generate in a model that is parented to nil, and then parent that model to workspace. Here is the video:


How can I make the loading faster?

Script:

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

Instead of making it faster, why not add a loading screen?

As far as I know, you couldn’t really make the chunks load faster efficiently.

The chunks loading is going to be slow eventually, so loading screen isn’t going to help that much.

I have seen voxel games load stuff faster than mine.

maybe lower the wait(0.5) at the end of the code?
try like 0.25

Try playing in game instead in roblox studio. Maybe studio causes it to load slower. Also change your wait(0.5) into task.wait(0.5)

1 Like

DO NOT use wait() it could fail anytime, instead use task.wait()

1 Like

I just ran the script in my studio, it loads way faster than it is for you.

what do you mean fail? never saw wait() failing

It could. It has been proven. task.

1 Like

Damn didn’t know that, thanks!

local Players = game:GetService("Players")
local BLOCK_SIZE = 3
local CHUNK_SCALE = 16
local RENDER_DISTANCE = 160
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 > 150 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 task.wait() 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
end

I lowered the wait all down to task.wait() and it still so slow. I want it to be faster! Do I need to rewrite the code, is it inefficient?


Didn’t know what happened to the recording :rofl:

you didnt know wait() was deprecated the entire time? LMAO

The solution was delete the destroy chunks function.