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.