I’m having a problem with ReadVoxels. When I generate a region3, it’s (presumably) positioned in the correct location. The code reads the terrain to be stored in the datastore, but even though the example piece is positioned within the terrain, it always returns the result as if all the materials in the table were “Air.” Any suggestions?
Code:
local RESOLUTION = 8
local MATERIAL_TO_INT = {}
local INT_TO_MATERIAL = {}
local DataStore = game:GetService("DataStoreService"):GetDataStore("Voxels")
local Size = Vector3.new(10,10,10)
local Position = Vector3.new(220,-100,50)
local ChunkSize = 400
local materials = Enum.Material:GetEnumItems()
for i, material in ipairs(materials) do
MATERIAL_TO_INT[material] = i
INT_TO_MATERIAL[i] = material
end
local function saveTerrain(position, ss, chunkSize)
local min = position - (ss/2)
local max = position + (ss/2)
local TIME = tick()
local key = {}
for x = min.X, max.X - 1, chunkSize do
for y = min.Y, max.Y - 1, chunkSize do
for z =min.Z, max.Z - 1, chunkSize do
local chunkMin = Vector3.new(x, y, z)
local chunkMax = Vector3.new(math.min(x + chunkSize, max.X), math.min(y + chunkSize, max.Y), math.min(z + chunkSize, max.Z))
print(chunkMin,chunkMax)
local region = Region3.new(chunkMin, chunkMax):ExpandToGrid(RESOLUTION)
local PArtRegion =Instance.new("Part",workspace)
PArtRegion.Position =region.CFrame.Position
PArtRegion.Size = region.Size
PArtRegion.Anchored = true
PArtRegion.Transparency = .5
local materials, occupancies = workspace.Terrain:ReadVoxels(region, RESOLUTION)
local size:Vector3 = materials.Size
local chunkData = buffer.create(RESOLUTION * size.x * size.y * size.z)
local chunkOffset = 0
local Can = false
for i = 1, size.X do
for j = 1, size.Y do
for k = 1, size.Z do
local occupancy:number = occupancies[i][j][k] * 255
local material:number = MATERIAL_TO_INT[materials[i][j][k]]
if material ~= MATERIAL_TO_INT[Enum.Material.Air] then
Can = true
end
print(materials[i][j][k])
buffer.writeu8(chunkData, chunkOffset, occupancy)
buffer.writeu8(chunkData, chunkOffset + 1, material)
chunkOffset += 2
end
end
end
if Can then
local keys ="Chunk_" .. x .. "_" .. y .. "_" .. z
print(keys)
key[keys]={chunkData,tostring(size)}
end
task.wait(0)
end
end
end
DataStore:SetAsync("TERRAIN",key)
print("Saved in:",tick()-TIME.."s")
end