How to decrease lag

Hello, I’m making a Minecraft-like game. And I’m experiencing a lot of lags. How to prevent this and how did Minecraft prevent this too.

Try shorten your code, if your code is really long, that just increases the work the server has to do.

Notes: I am not a professional, but that’s what I have been told.

Not too knowledgeable in regards to mitigating lag on Roblox, but some starters include reducing the number of unions you have (only incorporate necessary ones into your place) and decreasing the number of parts in general.

Vaguely familiar with how Minecraft works to prevent substantial lag, by which “chunks” are loaded at a set radius around the player (see here). Note that the “rest” of the world doesn’t exist yet until the player crosses a threshold, thus loading another chunk, and this is assuming the game is in single-player. That said, this system can reduce lag by only loading a pre-determined number of parts as the player moves around as opposed to having all the pieces there already.

Best of luck with your game!

local block_size = 3
local chunk_scale = 16
local render_distance = 20
local seed = math.random(-10e6,10e6)
local noise_scale = 30

local blocks_folder = game:GetService("ReplicatedStorage"):WaitForChild("blocks")

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,y,z,parent,block)
	if block then
		block:SetPrimaryPartCFrame(CFrame.new(x * block_size,y * block_size,z * block_size))
		block.Parent = parent		
	end
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 > 9 and density < 10 then
					mountLayer(cx, y, cz, chunk_object,blocks_folder:FindFirstChild("grass_block"):Clone())					
				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 > 90 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(1) do
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		if player.Character then
			local humanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")
			if humanoidRootPart then
				checkSurroundings(humanoidRootPart.Position)
			end
		end
	end
end

Everything is important, I can’t shorten it.

How much lag are your players getting from this piece of code? It looks pretty clean to me.

The amount of lag I am getting is not that bad, its just a loss of fps, however, I want it to be fast as original Minecraft

Use Yucon Framework to optimize your code
https://devforum.roblox.com/t/yucon-framework-next-level-optimizing-and-organizing

Can you send another link that link isn’t working

Sure

See his tutorial to learn properly

1 Like

I am using yucon framework, the laggyness didn’t changed

Did you watch the video? If you didn’t you have to watch it.

I did watch the videooooooooo.

Did you just import Yucon Framework and left it like that or you scripted a bit?

I scripted itttttttttttttttttttttttt

Well, test your game (not in studio) you will notice in game memory.

I can’t publish game so i can’t test it in game, I had this publishing problem few months now

Turn on streaming enabled, properties of workspace.

With this turned on your game will render chunks around your character in a certain radius instead of having to load the entire game.

I already have my own chunks generating system, do I need to use 2 of them?

Hmm, make a new place so you can publish your updates there.