✓ Accepted answer
Ideas:
- See if GetTouchingParts gets you all the parts you want (then just filter through them by color)
- Go through all spheres and filter by color and magnitude (maximum magnitude should be slightly higher than the diameter of a sphere). Ideally you would have a more complicated algorithm that only looked at spheres in certain "sectors", but so long as the total number of spheres is not too high it shouldn't be too much of an issue.
- Your Region3 idea is actually better than using magnitude, since Roblox knows better what parts are in what general areas. (You'll still want to use magnitude to make sure they're in range.)
So, your ideas are fine, but you note you had a stack overflow error. I'm going to guess you tried to get all the connected parts recursively (looking for a chain of, say, all the connected green spheres). There's nothing wrong with this, but you do need to include a table (as an argument to your recursive function) that contains all the spheres you've already looked at - otherwise you're going to be looking at the same two spheres over and over again!
Outline of avoiding stack overflow in recursion:
function recurse(sphere, adjacentList, seenDict)
adjacentList = adjacentList or {}
seenDict = seenDict or {}
seenDict[sphere] = true
local adjacentSpheres
for i = 1, #adjacentSpheres do
if adjacentSpheres[i].BrickColor == sphere.BrickColor and not seenDict[adjacentSpheres[i]] then
table.insert(adjacentList, adjacentSpheres[i])
recurse(adjacentSpheres[i], seenDict)
end
end
return adjacentList
end