When I just create the part it works perfectly and converts the terrain into 4x4 voxel(untextured and without the method I would like to do it) perfectly.
When I call the bindable event it is not wide enough and has some mistakes that is commented out.
region = region:ExpandToGrid(4)
local material, occupancy = game.Workspace.Terrain:ReadVoxels(region, 4)
local size = material.Size
for x = 1, size.X do
for y = 1, size.Y do
for z = 1, size.Z do
if material[x][y][z] ~= Enum.Material.Air then
--print("Material at (", x, y, z, "): ", material[x][y][z])
local pos = script.Parent:CellCenterToWorld(x,y,z)
local part = Instance.new("Part")
part.Size = Vector3.new(4,4,4)
part.Anchored = true
part.Parent = game.Workspace
part.Position = pos
--game.ServerScriptService.LegacyTerrainManager.PlaceTerrainBlock2:Fire(pos,"MegaGrass")
end
end
end
end
bindable event calls this function in the script. with norefresh, and autowedge is off.
function createPart(position,material,norefresh, autowedge)
local new = Instance.new("Part")
new.Size = Vector3.new(4,4,4)
new.Position = Vector3.new(GridPos(position.X), GridPos(position.Y), GridPos(position.Z))
new.Anchored = true
new.Locked = true
new.Parent = assignChunk(new.Position)
--***create part, apply properties
if new.Parent:FindFirstChild(getNameFromPos(new.Position)) then
new.Parent[getNameFromPos(new.Position)]:Destroy()
end
--***if found part at position, replace
new.Name = getNameFromPos(new.Position)
new.TopSurface = Enum.SurfaceType.Smooth
new.BottomSurface = Enum.SurfaceType.Smooth
--*** set name by position, apply surfaces
local isCovered = Instance.new("BoolValue", new)
isCovered.Value = false
isCovered.Name = "isCovered"
local Material = Instance.new("StringValue", new)
Material.Value = material
Material.Name = "TerrainMaterial"
local isTerrain = Instance.new("BoolValue",new)
isTerrain.Name = "IsTerrain"
local shape = Instance.new("StringValue", new)
shape.Name = "TerrainShape"
shape.Value = "Block"
--***assign values
new.Parent = assignChunk(new.Position)
--***assign part chunk (see function assignChunk)
if not norefresh then
local findabove = new.Parent:FindFirstChild(getNameFromPos(new.Position+Vector3.new(0,4,0)))
if findabove then
refreshPartCoverState(findabove)
end
local findbelow = new.Parent:FindFirstChild(getNameFromPos(new.Position-Vector3.new(0,4,0)))
if findbelow then
refreshPartCoverState(findbelow)
end
local findparts = Instance.new("Part", workspace)
findparts.Anchored = true
findparts.CanCollide = false
findparts.Locked = true
findparts.Position = new.Position
findparts.Size = Vector3.new(12,4,12)
findparts.Touched:Connect(function()end)
if findabove then
refreshPartCoverState(findabove)
end
if findbelow then
refreshPartCoverState(findbelow)
end
local founds = findparts:GetTouchingParts()
for i = 1, #founds do
if founds[i]:FindFirstChild("IsTerrain") and founds[i] ~= new then
refreshPartCoverState(founds[i])
end
end
findparts:Destroy()
end
assignTextures(new)
refreshPartCoverState(new)
if autowedge == true then autoWedgePart(new) end
end```