you can traverse the tree of nodes with recursion (function calls itself)
you can also google implementations of this because its a really common algorithm
example for if green node is connected:
create a function that checks if one node is connected to a green node
if none of the other nodes are green, then inside that function, call itself with the other nodes as the argument to check if those nodes are connected to a green node
skip the current node if its already visited
--example pseudocode, implement some parts of it yourself
--this visited table should be reset when isConnectedToGreen is called non-recursively
local visited = {}
local function isConnectedToGreen(node)
--tables in roblox can store anything as a key including instances, so this works
if visited[node] then return end
visited[node] = true
if thisNodeIsGreen then return true end
for _, child in ipairs(node:GetChildren()) do
--return true if any related node is green
if isConnectedToGreen(child) then return true end
end
--return false if no nodes in this entire tree is green
return false
end
What do you mean by for _, child in ipairs(node:GetChildren()) do?
I don’t think that’ll work for my case, because it can only go down the tree, to the children, not up the tree, to the ancestors. What else could I do? Thanks!