Hello there! This tutorial is meant for beginners, although anyone is more than welcome to follow along. This will cover referencing items and how relationships between the items are. Lets get started.
Parent-Child Relationships
Roblox uses the format Parent-Child or Hierarchy system with almost everything that shows/interacts with your game.
A child can be a parent, having something above, and beneath it like:
Think of it all as a family tree
Lets say, we have a parent with one child, named child 1, and that child also has a child, called child 2. What would the relationship between child 2 and parent be?
Well, we call the child, a descendant, and the parent, ancestor. This goes for any other children of child 2, or child 1.
“So why is this important”
Referencing
Firstly, open the explorer by navigating to the “View” tab, then pressing explorer. (If its not already open)
Now navigate back to the “Home” tab and find the “Part” button. Click it, and you should see a part come into view. This recently created part is a child of “Workspace”, which will house most, if not all, of your parts. It is also a descendant of “game”, which is the parent or ancestor of most-all items in your game. (Non script related)
So how would i tell a script, where my part is?
Easily, by following our child-parent relationship, we can determine where our part is:
Writing it to a script, we would do:
game.Workspace.Part
Think of the arrows as periods, going from our images to writing.
What if i change the name of my Part?
Simply, change the name of Part in your script also. Roblox will hand you a nice error if you forget to do so, or it could give you problems, if you needed that specific part to do something.
--Replace:
game.Workspace.Part
--With:
game.Workspace.MyPart --Your Part name would replace “MyPart”
Going a little off-stray, What if I have a space in the name of my part?
Well, not to cram your noggin, but there is another way of referencing parts.
game.Workspace[“My Beautiful Part”]
This way of referencing helps to get parts with spaces in the names.
game.Workspace[“My Part”].OtherPart
Yes, they can be used together.
What if i move my part somewhere else??
You would change the child of game, or Workspace in our example to the Parent that you moved it to.
For example, say we move the Part from Workspace to Server Storage. Writing to a script,
-- Replace:
game.Workspace.Part
--With:
game.ServerStorage.Part
Well, now you know how Roblox handles items, and how to reference items in a script. Still confused, ask any questions in replies and i’ll answer as fast as possible.
Thanks for reading through, and I hope it helped!