Which one is better? Looking to be more efficient
if Model.Humanoid then
or
if Model:FindFirstChild(“Humanoid”) then
Which one is better? Looking to be more efficient
if Model.Humanoid then
or
if Model:FindFirstChild(“Humanoid”) then
The dot index is going to be faster, but not significantly enough that you should use it for that reason alone.
The dot index will error if you attempt to index and invalid child, while FindFirstChild
will not error and will instead return nil
. Generally speaking, use FindFirstChild
when you’re not sure if an object exists or not, and the dot index when you are sure.
In the scenario above, you’d likely want to use FindFirstChild
as you’re checking IF a child exists.
This isn’t really a question of efficiency as it is about usability.
iirc FindFirstChild is just shorthand for a loop that goes through every child and returns the first one it finds with the same name as the thing you put in parentheses
FindFirstChild returns nil if the loop never finds it while doing it directly with a period errors.
Therefore, FindFirstChild is good if you need to find if an object exists without errors. But it’s always going to be slower, depending on how many children the loop has to go through.
FindFirstChild
is basically just a check to make sure that you have the property, otherwise it returns back as nil
as Timothy stated earlier
Using Model.Humanoid
alone would think that the script has already found the humanoid without any sort of check, which would potentially result in some errors
To summarize: Use FindFirstChild
for safety precautions, it’s better