Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I read about FindFirstChild() and figured out it will make my performance worse, how can i fix?

I read it from wiki, I really like FindFirstChild() and use it a lot. But i scared my performance will be bad. What should i do?Any good built-in functions/events i can use to replace FindFirstChild()?(except WaitForChild()) The source of my information: https://developer.roblox.com/en-us/api-reference/function/Instance/FindFirstChild "Performance Note

FindFirstChild takes about 20% longer than using dot operator, and almost 8 times longer than simply storing a reference to an object. Therefore, you should avoid calling FindFirstChild in performance dependent code, such as in tight loops or functions connected to RunService.Heartbeat/RunService.RenderStepped. Store the result in a variable, or consider using ChildAdded or WaitForChild to detect when a child of a given name becomes available."

3 comments

0

FindFirstChild is mainly used when there is a risk that the specified reference object may be nil. In this case, the FindFirstChild will simply return nil and will not terminate the program upon error.

0

The WaitForChild method will yield the program until the referenced object becomes available; it will wait until the instance exists if it appears to have not found it.

0

This is why it is advised to be using WaitForChild as a program that is strictly dependent on requiring the referenced objects or declared allocations as if it accidentally tries to perform an action on a nil instance it will stop running and possibly cause issues down the road through collateral.

1 answer

There is no "fix" You just gotta use your head and use it as efficiently as possible if you want the best performance

e.g

-Only use it when theres a chance the part doesnt exist when the code run

-Dont use it inside a loop or something like that

This discussion is preserved as it was. New answers, comments, and votes are closed.