Avoiding a table overflow

I am currently creating a voxel generation script (A little like Minecraft)
and storing all my blocks in a 3D matrice like that:

MainTable = {}
for x=startpos.X,endpos.X,1 do --Caves
MainTable[x] = {}
		for z=startpos.Z,endpos.Z,1 do
               MainTable[x][z] = {}
			for y=0,180,1 do
                       MainTable[x][z][y] = "whatever value"
                       end
             end
end

But after creating a large terrain piece I got the table overflow error, I suppose this means that the table is just too big for Roblox.

But since I want to have an infinite world, I cannot find a fix to my problem, so if you have any idea on how I would get around this?

Don’t store blocks in a table. Use seed-based generation for it. Any changes to the world can be saved using a chunk system, with each chunk (probably 16x16x16) getting its own table.

1 Like

Do you actually need to store the data in a table? Could you just destroy it after players get too far away/its not needing to be changes anymore, and then use an algorithm (like math.noise) to generate it again?

Well I have a seed-based generation divided in chunks, but how would I make it so each chunk has its own table?

It’s a matter of storing block to then generate structures like trees ect, so they can generate acroos chunks

I’m pretty sure the max amount in a table is 100M

Gonna necrobump a bit in case someone else ends up looking for an answer like me. I needed to know how big of a heightmap I can use for custom pathfinding.

The size limit for arrays is 2^26 (67108864). Which would be a 8192x8192 2D array.
The size limit for hash tables is 67108865. Though note that these 2 components are completely separate, so you can store both 67108864 values as an array and 67108865 as a hash table in one table.

This is the code I used to test it. It requires a bit of a beefy CPU to run though. Takes a minute or 2 to complete on a 5800X3D.

local o = table.create(67108864, 1)
task.wait(5) -- prevent early crash from too high usage
print("STARTING")
local i = 1
local _,msg = pcall(function()
	local lt = os.clock()
	while true do
		if os.clock()-lt>1 then
			task.wait()
			print(i)
			lt = os.clock()
		end
		for _=1,5000 do
			o[tostring(i)] = 1
			i+=1
		end
	end
end)
print("ENDED AT "..tostring(i))
print("ERR: "..msg)
1 Like