How can I remove all terrain except water

I have a simple question, but one I could not find a solution to.

Let’s say I have a cube of terrain with water in it.

Now I want to (with a script) remove all the terrain except water in the cube.

I’m making a terrain gun that can destroy terrain when you shoot it at something, but I don’t want it to destroy water. Any help here? Thanks.

You aren’t looking the right way. Almost headed into XY problem grounds and I was going to point that out until I saw your use case,

Leave the terrain water in, don’t touch terrain. Ray methods support a way to ignore water terrain but capture any other terrain. For example, the most primitive raycast method, FindPartOnRay.

local ray =
local ignoreTable = {instanceListAndTheirDescendantsToIgnore}

local A, B, C, D = workspace:FindPartOnRay(ray, ignoreTable, false, true)

If you specify the last parameter IgnoreWater as true, it’ll do just that.

Thank you for your reply, but it’s not exactly what I’m looking for, I don’t know much on the subject of rays so I had to do some researching but I got to print what terrain material the gun was shooting at. The only problem is that my gun works like the terrain tools, having to dig out a sphere of terrain, so I want it so if I click right next to some water, it will hollow out a sphere of terrain but keep the water, I hope that makes sense.

You can use ReadVoxels and WriteVoxels functions to achieve that.

If your gun is making spherical holes, you can just create a new Vector3 from XYZ properties of a voxel and check the magnitude from the point it hit (and change if it matches the criteria).

So you can probably use this thread to help you and you can reverse it so if the material isn’t water, then it will clear the terrain. Also in the replies is another pretty good suggestion and it does something like
if materials[x][y][z] == Enum.Material.Water then
and instead of doing that, you would do
if materials[x][y][z] ~= Enum.Material.Water then
So it will get everything that isn’t water and set it to air.