Finding closest object of certain material?

I’m trying to add a dungeon to a random position in the overworld, on top of a specified material, within like 50-150 studs away from player.

Getting all descendants of workspace and getting the magnitude of everything sounds kind of heavy for the engine, any ways around that?

And what about terrain materials? Raycasts from above on each stud on 150x150 area equals 22500 raycasts, not really efficient either… :roll_eyes:

https://developer.roblox.com/en-us/api-reference/function/WorldRoot/FindPartsInRegion3

Use the above function to find all the parts in any given Region3 value, then all you need to do is check the material of each part by doing:

if part.Material == Enum.Material.Plastic then --plastic is an example
	--do code
end

You can use the character’s current position to offset the Region3 if necessary.

Does this work with terrain voxels?

Apparentally yes, thanks, this’ll be handy.

When you mentioned material I thought you were referring to the material property of parts.

Its suggested to use the new spatial query API over Region3

Objects WorldRoot:GetPartBoundsInBox(CFrame cframe, Vector3 size, OverlapParams overlapParams)

Objects WorldRoot:GetPartBoundsInRadius(Vector3 position, float radius, OverlapParams overlapParams)

1 Like