Replace terrain material under a part


I’m working on a building system for my game, and I want structures like Floors to replace the terrain material under them from Grass to Mud.

The structures have a primary part that covers their entire model like this.

I wanna use this primary part to replace any grass under itself to mud, so after looking at some docs and examples, I’ve came up with this code, but it has no effect on the terrain what so ever.

local part = script.Parent;
local StudsUnder = 1;
local region = -, StudsUnder, 0))  -  (part.Size/2),  (part.Position -, StudsUnder, 0)) + (part.Size/2));
workspace.Terrain:ReplaceMaterial(region, 4, Enum.Material.Grass, Enum.Material.Mud);

I should probably note that I don’t understand regions very well so I’m not sure if the region I’ve came up with is even correct.

Could you guys tell me how you would go about this?

1 Like

I think it’s about the order of the operations make it so the substraction is made before and then it does the addition

I’m afraid I don’t really understand

Forget that, you did that already, try to increase the studs under parameter

1 Like

Found out why it doesn’t work:

Before replacing materials just do:
Region = Region3:ExpandToGrid(Region)

1 Like

Forgot to mention but I added the ExpandToGrid function to the code after the post.
But! Increasing the StudsBelow value to a number like 5 seems to do the trick, so thank you. : )