How to delete terrain in a specific area with code

Is there a way to delete certain terrain in a certain area with code? I know how to check a certain region and that Workspace.Terrain:Clear() clears all the terrain, but they don’t really combine well to achieve the desired effect. for i,v in pairs sounds tempting, but there is no table lol. Any help is appreciated!

1 Like

Hello Thereasonableplayer!

You can clear a certain region of terrain using game.Workspace.Terrain:FillRegion() and fill it with Enum.Material.Air

Hope this helps!


Really helpful insight! Don’t want to be greedy, but it is kinda half of what i was going for lol. So for my game for some reason the terrain has the ability to impact and ruin pathfinding. I noticed that the main problem it is due to the really really small pieces of rocky terrain that I accidentally generated in infinite space lol(Which means it is so difficult to locate them by scrolling around with my mouse).

local terrain = workspace.Terrain
local part = script.Parent–This part is used to cover the terrain that I don’t want deleted
local function insideRegion(peskyterrain)–Got this code from a bird man(Birdelther) a while ago I didn’t make this
local size = part.Size
local partCFrame = part.CFrame
local v3 = partCFrame:PointToObjectSpace(peskyterrain)
return (math.abs(v3.X) <= size.X / 2) and (math.abs(v3.Y) <= size.Y / 2) and (math.abs(v3.Z) <= size.Z / 2)

for i,v in ipairs(terrain) do – Trying to find a way to loop through terrain lol, but to no avail
if not insideRegion(v.Position) then

Guess the other main question is how do you loop through terrain lol

Well you could loop just before deleting the parts right?

The problem is that there is no table for terrain lol Im just giving a sample convenient code if there was a table of all the terrains parts inside.

I don’t have much experience with terrain but is there a way where you can just replace terrain with air? pretty sure you can do that. Just use the fill script and fill Air

1 Like