Correction to my earlier reply: the code snippet in the original post assumes the MaterialVariants it names already exist, so it errors with MaterialVariant 'SwampVariant' not found for the specified base material if you run it as-is. Here is a corrected version.
A few things to know:
- The third argument of
SetMaterialSlotmust be the name of a MaterialVariant that already exists inMaterialServicewith the same base material. Otherwise you’ll getMaterialVariant '<name>' not found for the specified base material. - To create a plain tinted material with no MaterialVariant, pass an empty string (
""). - The name you give a material in the Terrain Material Manager is a display name only. It isn’t the variant name, so don’t pass it to
SetMaterialSlot. GetMaterialSlotreturns the base material, the MaterialVariant name (""if none), and the color. It returnsnilfor an empty or reset slot.- Slots
0and1are reserved for Air and Water. Slots2–63can be set.
local Terrain = workspace.Terrain
local MaterialService = game:GetService("MaterialService")
-- SetMaterialSlot needs the named MaterialVariant to exist first.
-- Normally you'd create these in the Material Manager; here they're created in code.
local function ensureVariant(name, baseMaterial)
local variant = MaterialService:FindFirstChild(name)
if not variant then
variant = Instance.new("MaterialVariant")
variant.Name = name
variant.BaseMaterial = baseMaterial
variant.Parent = MaterialService
end
return variant
end
ensureVariant("SwampVariant", Enum.Material.Grass)
ensureVariant("AlienVariant", Enum.Material.Rock)
-- Create material slots
Terrain:SetMaterialSlot(22, Enum.Material.Grass, "SwampVariant", Color3.fromRGB(50, 60, 30))
Terrain:SetMaterialSlot(23, Enum.Material.Rock, "AlienVariant", Color3.fromRGB(100, 0, 255))
-- A slot without a MaterialVariant: pass "" to tint the base material
Terrain:SetMaterialSlot(24, Enum.Material.Grass, "", Color3.fromRGB(200, 170, 60))
-- Get material slot information for printing it
local baseMaterial, variantName, color = Terrain:GetMaterialSlot(22)
print(("Slot 22: %s / %s / (%d, %d, %d)"):format(
tostring(baseMaterial),
variantName,
math.round(color.R * 255),
math.round(color.G * 255),
math.round(color.B * 255)
))
-- Use a region that aligns cleanly to the resolution grid
local resolution = 4
local region = Region3.new(Vector3.new(0, 0, 0), Vector3.new(64, 16, 64)):ExpandToGrid(resolution)
-- Compute the actual voxel dimensions from the snapped region
local size = region.Size / resolution
local sizeX = math.round(size.X)
local sizeY = math.round(size.Y)
local sizeZ = math.round(size.Z)
-- Build 3D nested arrays [x][y][z] for writing the new slots
local materialIndices = {}
local occupancy = {}
for x = 1, sizeX do
materialIndices[x] = {}
occupancy[x] = {}
for y = 1, sizeY do
materialIndices[x][y] = {}
occupancy[x][y] = {}
for z = 1, sizeZ do
occupancy[x][y][z] = 1
materialIndices[x][y][z] = if (x + z) % 2 == 0 then 22 elseif x % 2 == 0 then 23 else 24
end
end
end
Terrain:WriteVoxelChannels(region, resolution, {
SolidMaterialIndex = materialIndices,
SolidOccupancy = occupancy,
})
-- Reset slot 23: voxels referencing it render as invalid
Terrain:ResetMaterialSlot(23)

