Hi, I’m trying to make a voxel game with 1 dimensional tables to store chunks
It works fine when creating flat terrain, but when i remove one block inside a flat surface it is messed up:
this is what happened when i deleted a block at the coordinate (5,1,5).
here is my code:
local v = require(game.ReplicatedStorage.VariablesServer)
local mapsize = 2
local chunksize = 16
local speed = 50
local cheight = 16
local height = 5
local seed = 69420
local map = {}
local sides = {
{1,0,0}, -- right
{-1,0,0}, -- left
{0,0,1}, -- forward
{0,0,-1}, -- backward
}
local function queueBlock(t,c)
table.insert(c,t)
end
local function blockOffset(x,y,z)
return (x + (chunksize) * (z + (chunksize) * y)) +1
end
local function findBlock(x,y,z)
local off = blockOffset(x,y,z)
if x > chunksize-1 or z > chunksize-1 then
return false
end
return off
end
local function createBlock(t,x,z,x1,z1,y,f,n)
local function resizeBlock(b,v,sx,sy,sz)
b.Size += Vector3.new(math.round(math.max(sx-0.5,0)), sy, math.round(math.max(sz-0.5,0)))*4
--b.Position = v + Vector3.new(sx/2,sy/2,sz/2)
end
local block = v.rep.Blocks:FindFirstChild(t)
local clone = block:Clone()
clone.Parent = workspace.Map
local sx = (x1-x)
local sz = (z1-z)
clone.Position = Vector3.new(x+sx/2+0.5,y,z+sz/2+0.5)*4
--clone.Size = snapv(Vector3.new(sx+0.5,1,sz+0.5),1)
resizeBlock(clone,clone.Position,sx,0,sz)
clone.Parent = f
if t == "Debug" then
clone:WaitForChild("BillboardGui").TextLabel.Text = tostring(n)
end
end
local function getChunk(px,pz)
return (pz*mapsize)+px+1
end
local function createBlockAtGrid(t,x,y,z)
local px = math.floor(x/chunksize)
local pz = math.floor(z/chunksize)
local chunkdata = map[getChunk(px,pz)]
local chunk = chunkdata[2]
chunk[blockOffset(x,y,z)] = t
meshing(px,pz,chunkdata[1],chunk)
end
function meshing(px,pz,folder,chunk)
folder:ClearAllChildren()
local startx = 0
local startz = 0
local endx = 0
local endz = 0
local begin = true
local ox = (px*chunksize)
local oz = (pz*chunksize)
local zlast = 0
local zfound = false
local merges = 0
for y=0, chunksize-1 do
for x=0, chunksize-1 do
for z=0, chunksize-1 do
if chunk[findBlock(x,y,z)] then -- found block
if begin then
startx = x
startz = z
zlast = 0
zfound = false
begin = false
end
if not zfound then
if not chunk[findBlock(x,y,z+1)] and z+1 < 15 then -- no found neighbor for z
zlast = z -- found last z, now searching x
zfound = true
end
if z+1 > 15 then -- at edge of chunk
zfound = true
zlast = z -- found last z, now searching x
end
end
if zfound then -- search z axis because last x is found
if not chunk[findBlock(x+1,y,z)] and x+1 < 15 then -- no found neighbor for x
endx = x
begin = true
merges += 1
createBlock("Debug",startx+ox,startz+oz,endx+ox,zlast+oz,y,folder,merges)
break
end
if x+1 > 15 then -- at edge of chunk
endx = x
begin = true
merges += 1
createBlock("Debug",startx+ox,startz+oz,endx+ox,zlast+oz,y,folder,merges)
break
end
end
end
end
end
end
end
function snapv(v,d)
return Vector3.new(math.round(v.x/d)*4,math.round(v.y/d)*4,math.round(v.z/d)*4)
end
local function snap(n)
return math.round(n/4)*4
end
local function createMap()
for z=1, mapsize do
for x=1, mapsize do
table.insert(map,{})
end
end
end
local function createChunk(px ,pz)
local chunkFolder = Instance.new("Folder")
chunkFolder.Parent = workspace.Map
chunkFolder.Name = "chunk_"..px.."_"..pz
local chunk = {}
for y=0, chunksize-1 do
for z=0, chunksize-1 do
for x=0, chunksize-1 do
queueBlock(y==1,chunk)
end
end
end
--queueBlock(true)
--queueBlock(true)
--queueBlock(true)
meshing(px,pz,chunkFolder,chunk)
map[getChunk(px, pz)] = {chunkFolder,chunk}
end
createMap()
createChunk(0,0)
createBlockAtGrid(false,5,1,5) -- destroy one block on flat surface
I would like to know the problem and what code i should add to fix it. thanks in advance


