Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How Do I make a part that paints terrain that it collides with?

I made a script:

game.Workspace.Terrain:ReplaceMaterial(game.Workspace.fill.CFrame, game.Workspace.fill.Size, Enum.Material.Mud)

but it doesn't work and I don't understand this.

1 answer

Here I cover 2 Solutions: Solution 1: As far as my understanding, The dev hub says that the parameters for ReplaceMaterial() are: region (given), resolution (given), sourceMaterial (not given) & targetMaterial (given as sourceMaterial).

If you go to https://developer.roblox.com/en-us/articles/Scripting-With-Terrain and go to the Reading and writing voxels section, it explains that terrain is split up into voxels with their own materials.

There is also a loop that goes through each voxel in a region and gives you the Material (We can use this) and the Occupancy (we don't need). You could adjust this loop fit your needs (Change the region and run ReplaceMaterial() for each voxel, using the voxel material for the sourceMaterial parameter).

local region = Region3.new(Vector3.new(0,0,-15), Vector3.new(4,8,4))
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
			print("Material at (", x, y, z, "): ", material[x][y][z])
			print("Occupancy at (", x, y, z, "): ", occupancy[x][y][z])
		end
	end
end

Solution 2: As far as my understanding, The dev hub says that the parameters for ReplaceMaterial() are: region (given), resolution (given), sourceMaterial (not given) & targetMaterial (given as sourceMaterial).

You could run a loop with ReplaceMaterial() in it, cycling through all terrain materials to use for the sourceMaterial Parameter.

Here are all the Terrain Materials: https://developer.roblox.com/en-us/api-reference/enum/Material

This discussion is preserved as it was. New answers, comments, and votes are closed.