Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Difference between IsDescendantOf and second parameter in FindFirstChild?

Asked by 5 years ago

From what I know, they seem the exact same. They search through all of the descendants of the object until they find what they are looking for. Can anybody explain to me a scenario where you would use one over the other?

1 answer

Log in to vote
1
Answered by
nilVector 812 Moderation Voter
5 years ago
Edited 5 years ago

The IsDescendantOf function of Instance returns a boolean, meaning if the Instance you are checking is the descendant of the other Instance you specified, it will return true, otherwise it will return false.

FindFirstChild, specifically regarding its second parameter being true, will return the Instance that it is an ancestor of with the specified name or nil if no such Instance could be found.

I think the confusion you're getting is that you can use both A:FindFirstChild(B.Name, true) and B:IsDescendantOf(A) equivalently, but that's only true in conditional statements. The reason why A:FindFirstChild(B.Name, true) will work in conditional statements is because Lua does not evaluate just boolean statements in its conditionals but also truthy and falsy values. An Instance that exists (and isn't nil) is known to be truthy, while an Instance that does not exist (thus is nil) is said to be falsy. Hence, A:FindFirstChild(B.Name, true) will evaluate pass a conditional, for an existing Instance exists (in this case one whose name matches B.Name).

0
Oh, now that I see this answer I understand. IsDescendantOf() returns a boolean if it was found or not, but FindFirstChild() returns the actual instance. Thanks :D Operation_Meme 890 — 5y
Ad

Answer this question