Terrain Material Changing

local originalMaterials = {}

local function updateSmoothPlastic()
	for _, model in ipairs(game.Workspace:GetDescendants()) do
		if model:IsA("BasePart") then
			originalMaterials[model] = model.Material
			model.Material = Enum.Material.SmoothPlastic
		elseif model:IsA("MeshPart") or model:IsA("UnionOperation") then
			for _, mesh in ipairs(model:GetDescendants()) do
				if mesh:IsA("BasePart") then
					originalMaterials[mesh] = mesh.Material
					mesh.Material = Enum.Material.SmoothPlastic
				end
				if mesh:IsA("Texture") then
					originalMaterials[mesh] = mesh.Texture
					mesh.Texture = ""
				end
			end
		end
	end
end

local function revertToOriginal()
	for part, material in pairs(originalMaterials) do
		part.Material = material
	end

	originalMaterials = {}
end

I have a settings system, when you toggle the setting it makes all resource textures turn to plastic, and when toggled off it returns to it’s original state. How can I achieve this with Terrain, I am not quite sure how I can achieve this goal, since I have not programmed with such based around terrain.

2 Likes

Make use of these functions:

local Terrain = workspace.Terrain
local originalTerrainData = nil

local function saveOriginalTerrainData()
	local region = Terrain.MaxExtents
	originalTerrainData = Terrain:ReadVoxels(region, Enum.CellSize.Fifty)
end

local function updateToSmoothPlastic()
	local region = Terrain.MaxExtents
	local material = Enum.CellMaterial.SmoothPlastic
	Terrain:FillRegion(region, Enum.CellSize.Fifty, material)
end

local function revertToOriginal()
	if originalTerrainData then
		local region = Terrain.MaxExtents
		Terrain:WriteVoxels(region, Enum.CellSize.Fifty, originalTerrainData)
	end
end

Then, you can call these functions whenever you need:

saveOriginalTerrainData() -- Saves the terrain you have
updateToSmoothPlastic() -- changes terrain to plastic
revertToOriginal() -- bring back the previous terrain setup you had before changing to plastic

Let me know if this isn’t what you were looking for, or you need anything else!