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