# How can I detect if there is any terrain is in a certain part?

Hey guys, I’ve been having some problems figuring out how I can have a certain part detect if there is any terrain in it. I’m working on a new spawn system for one of my games. Right now all my parts spawn on a random position “on top” of a part. But I wish too make it so. All the parts I want to spawn in one area spawn on each highest point “on” the terrain. Example:
(This is what players would see when the game runs.)
On the left you can see what I want to achieve. The right is what i figured out.

(This is what my studio view should look like.)
On the left you see a pink regional part that’s covering up a piece of terrain. When the game runs. It searches for any terrain in the part and spawns whatever I want on the highest point of the section. On the right side was how my old system was. But was for parts and checked for the TopSurface.

If you don’t understand what I mean. You may reply and I’ll respond back asap. Any thoughts are welcome!

5 Likes

You can try using `ReadVoxels` to see if it returns anything.

I’ve tried using ReadVoxels many times but I can’t get it working. If someone is able to write me even the smallest script. That would be appreciated. Thanks.

Hmm, in order to achieve the highest point of the terrain why not cast a ray to find the surface position and place the part there?

Edit: NVM, I see your point of highest point in one area, have you tried reading and experimenting with what the ReadVoxels returns in the article?

``````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
``````

You can probably try to find the occupancy at the highest y coordinate and use that as a place to spawn the part, but there is a chance there might be multiple occurrences of the highest point which you might need to store in a seperate table.