Automatically clear the grass going through your parts!

A script I made which I found useful for myself so I decided to share it.

So recently, Roblox introduced the new grass feature which can be turned on through the “Decoration” property in the Terrain object.

The grass looks great, but since most developers didn’t build their map with the new grass in mind, I decided to create a script that removes the blades of grass that clip through the parts around your map.

Not sure if Roblox will end up integrating a feature for this directly, but for now, I have made this script as a workaround.

Here’s a before and after using the script:

As you can see, the grass is going through the parts.

This is after using the script, and as you can see the parts don’t have any grass going through them anymore.

How it works:

Basically, the script changes the terrain material under the parts from “Grass” to “LeafyGrass” (using Region3) which removes the grass from that area.

Heres the script:

local function regionPaintPart(p, yC, padding)
	--each part is divided into smaller regions (each with a size of 4x4 studs) since some parts are rotated and regions cant be rotated
	padding = padding or 0
	local s = p.Size+Vector3.new(padding*2,0,padding*2)
	for x=1, s.X/3 do
		for z=1, s.Z/3 do
			if ((z+x)%200)==0 then
				wait()
			end
			local p2c = CFrame.new(p.CFrame:PointToWorldSpace(Vector3.new(x*3,yC,z*3)-Vector3.new(s.X/2,0,s.Z/2)))
			local p2s = Vector3.new(5,5,5)
			local r = Region3.new(p2c:PointToWorldSpace(-Vector3.new(p2s.X/2,5,p2s.Z/2)), p2c:PointToWorldSpace(Vector3.new(p2s.X/2,5,p2s.Z/2)))
			workspace.Terrain:ReplaceMaterial(r:ExpandToGrid(4) ,4,Enum.Material.Grass, Enum.Material.LeafyGrass)
		end
	end
end

local function FixGrassPart(p, pad)
	regionPaintPart(p, 0, pad)
end

local function FixAllGrassParts(pad)
	for _, v in pairs(workspace:GetDescendants()) do
		if v:IsA("BasePart") and not v:IsA("MeshPart") and not v:IsA("Terrain") and v.Size.Y < 10 then
			print(v)
			if (_%100)==0 then
				wait() --Waits every 100 parts so it doesn't freeze studio
			end
			FixGrassPart(v, pad)
		end
	end
	print('FINISHED')
end

wait()

FixAllGrassParts(2)

You can just paste this script into the console while in edit mode on Studio and run it to let it do its thing.

There might be a more efficient way to do this which I’m not aware of, but this works fine if you give it some time to run.

151 Likes

Do these have the same texture?

5 Likes

Those materials have similar textures and if you give both materials the same color, the difference will be barely noticeable.

4 Likes

Wouldnt it be better to change the material to air instead of leafy grass?

Leafy grass not having geometry grass is a temporary solution to this problem to my knowledge.

3 Likes

The reason I didn’t choose Air is because it would create gaps where the terrain is supposed to be since roblox terrain uses a 4x4 stud voxel grid for the cells and parts usually aren’t aligned with that voxel grid. Replacing the terrain material would still keep the same terrain that there was while only removing the geometric grass.

Edit:
Then again, if anyone would like to use any other material instead of LeafyGrass, you can change it from this line:

workspace.Terrain:ReplaceMaterial(r:ExpandToGrid(4) ,4,Enum.Material.Grass, Enum.Material.LeafyGrass)
12 Likes

Thank you so much! I was recently working on a city commission planning to use Grass around the city with decoration enabled. This will make my job much easier :slight_smile:

6 Likes

Leafy grass currently has the grass disabled, this isn’t the best solution but it can just be changed to ground, mud or whatever you want instead and it should be fine.

1 Like

I dont understand this, help pls and why part.size/3?

This is so a small region3 is created for each 3x3 stud area of a part.

2 Likes

Wow, this is very useful. Thank you for this, I was wondering how to fix this issue.

4 Likes

Yeah, setting it to air wouldn’t work, here’s a quick thing I made to demonstrate.


8 Likes

Made it into a plugin! I made it so there is material settings so that you can customize what the grass gets changed to. And added a option to fix the part or model you have selected. https://www.roblox.com/library/4426181582/Grass-Remover

20 Likes

Interesting idea, I appreciate it!

6 Likes