So i tried to make a render system to remove the lag from my voxel game, but it's very laggy

Well tried to make the render sytem for my voxel game but the render system is very laggy because of those loops, and i dont know how i can get rid of those loops.

local char = script.Parent
local root = char:WaitForChild("HumanoidRootPart")
local blocksLib = require(game.ReplicatedStorage._LIBRARY_LIST.BLOCKS_LIB) -- The module that has terrain generation system.
local humanoid = char:WaitForChild("Humanoid")

local renderDistance = 70 -- Render distance

local RenderedBlocks = {} -- A table of all the  rendered blocks.

while wait(1) do -- waits until terrain generation is finished
	result = blocksLib.hasFinishedbase() -- returns if chunk olading is finished or not
	if result == true then
		break
	end
end
print("loaded")

local blocks = blocksLib.GetBlocks() -- Returns the terrain generation blocks.
while wait(0.7) do  -- Loop
	if root ~= nil then
		for i,v in pairs(blocks) do -- Get all the blocks
			if (v.Position - root.Position).magnitude <= renderDistance then
				if not table.find(RenderedBlocks,v) then -- If it isn't rendered
					table.insert(RenderedBlocks,v) -- Insert the block on the rendered blocks table
					v.Parent = workspace.Blocks -- The block parent is on workspace
					local direction1 = Vector3.new(0,-5,0) -- Detect if the block needs cast shadow.
					local raycastResult1 = workspace:Raycast(v.Position, direction1)
					if raycastResult1 then
						v.CastShadow = false
					end
				end
			end
			if (v.Position - root.Position).magnitude >= renderDistance then
				if table.find(RenderedBlocks,v) then -- if it is rendered
					local vNumber = table.find(RenderedBlocks,v)
					table.remove(RenderedBlocks,vNumber)-- remove block from rendered table
					v.Parent = game.ReplicatedStorage.World_Parts_ -- The block parent is the folder on replicated storage
				end
			end
		end
	else
		break
	end
end

Why are you using this system over streamingenabled,

also making the bold assumption this is minecraft related meaning this is also taking into account 70 blocks downwards as well (if its about that) incorporate raycasting into your code so it only shows what you see.

1 Like

I’ll check the streaming enabled , thanks for the help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.