Strange WriteVoxels Error

Hello. I’ve been trying to write some terrain plugins, and so I need to know how to use WriteVoxels, and ReadVoxels. I wrote some code that will fill an entire part with terrain, just to test my understanding of the :WriteVoxels() function.

local p = workspace.Part
local newRegion3 = require(script.Region3Helper)

local mats = {}
local occs = {}
for x = 1, p.Size.X / 4 do
	for y = 1, p.Size.Y / 4 do
		for z = 1, p.Size.Z / 4 do
			mats[x] = mats[x] or {}
			mats[x][y] = mats[x][y] or {}
			mats[x][y][z] = Enum.Material.Grass
			occs[x] = occs[x] or {}
			occs[x][y] = occs[x][y] or {}
			occs[x][y][z] = 1
		end
	end
end
workspace.Terrain:WriteVoxels(Region3.new(p.Position - (p.Size/2), p.Position + (p.Size/2)), 4, mats, occs)

So far, this code works. But if I have a part that has a size of 16, 16, 20, it will error. It will expect a 4 element array in materials[1][1], and the table that is inserted into this function has 5 elements. Why? Why does it expect a 4 element array, and not a 5 element array, and how can I fix this? 20 divided by 4 doesn’t have a remainder, so it should be possible to fill a part of this size. I noticed that if the quotient of dividing the size of the part along any axis by 4 is not divisible by 2, then it will error, and 20 (the size of the part along the Z axis is 20), divided by 4 is equal to 5, and 5 cannot be divided by 2 without obtaining a remainder, and it appears that this is what is causing the error, but again, I do not understand why this is an issue, because 20 is still divisible by 4.

I managed to find a solution to this problem, by just reading the voxels in the region of the part, and then overwriting the data returned from the readVoxels with occupancies of 1, and materials of grass.

local p = workspace.Part
local region = Region3.new(p.Position - (p.Size/2), p.Position + (p.Size/2))
local mats, occs, size = workspace.Terrain:ReadVoxels(region, 4)
for x = 1, occs.Size.X do
	for y = 1, occs.Size.Y do
		for z = 1, occs.Size.Z do
			mats[x] = mats[x] or {}
			mats[x][y] = mats[x][y] or {}
			mats[x][y][z] = Enum.Material.Grass
			occs[x] = occs[x] or {}
			occs[x][y] = occs[x][y] or {}
			occs[x][y][z] = 1
		end
	end
end

workspace.Terrain:WriteVoxels(Region3.new(p.Position - (p.Size/2), p.Position + (p.Size/2)), 4, mats, occs)

Considering the solution I had to use to solve this problem, I think that roblox should replace the workspace.Terrain:WriteVoxels(region, resolution, materials, occupancies) function with a newer one that is way easier to use, that doesn’t have ridiculous issues like this.

But if someone has anything else to contribute, please do, because I still don’t understand what caused this problem.

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