Script Performance

I am wanting to know how to script better. I want to know which is better.

Part:WaitForChild("Parent") or Part.Parent

workspace.Part or game.Workspace.Part

:WaitForChild is just used if the .Part doesn’t work that means if its used by a client. workspace and game.Workspace are the same.

1 Like

These are the best you can use.

1 Like

Part:WaitForChild("Parent") makes no sense whatsoever… As pointed out below it is possible, but I still don’t think it’s a good idea to name child parts the same as a property of the parent, as it’s confusing to read and forces you to use another method to access them

Direct indexing using . is faster, however, it will throw an error if the child hasn’t yet loaded, so this is when you would use WaitForChild(), but you should only use it when you are expecting something to load else it could be waiting indefinitely.

  1. You can add timeouts to WaitForChild().
  2. Part:WaitForChild("Parent") searchs for a part called “Parent” whose parent is “Part”, Part.Parent gets the parent of Part.
  3. You are able to use Part["Parent"] too and it returns same than Part.Parent.
  4. You should use pcall() if it’s able to error.
  5. “Workspace” is a property of “game”, so it (should) never error if you put game.Workspace on a script. Still I prefer using “workspace”

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.