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

can someone please explain what is indexing and ancestry in roblox lua?

Asked by 6 years ago

i get that sometimes i need to write game.workspace sometimes in my script but what does indexing mean exactly?

2 answers

Log in to vote
2
Answered by
kapipi12 133
6 years ago

"game.workspace.something" is like a path. When you want to for example identify where is a part, you have to use path so game.workspace.localization of your part. It does have a lot of other ways but these are the simplest one. Also, sometimes it needs to get where is something by script so you can get it by for example script.parent. It means, parent of your script is getting identified and you can do an action with it after identification it. If you got any more questions try to learn LUA from tutorials and maybe read something from Wiki, it helps.

For example:

local tool = script.Parent
-- That means, you are getting path of the tool, it is parent of the script (here of course).
0
is indexing like going down different floors of an elevator? EzireBlueFlame 14 — 6y
0
No, imagine an upside down tree. The trunk is game. One of the brances is Workspace, and so forth. LisaF854 93 — 6y
0
branches* LisaF854 93 — 6y
Ad
Log in to vote
1
Answered by 6 years ago

Indexing in this context is just getting a reference to an Instance (such as a Part, ParticleEmitter, or anything else within Roblox) so that you can do things with it, like moving it around, changing the color, destroying it etc.

Before explaining game.Workspace, I'll explain the concept of ancestry with another example.

Say you have a Model composed of Parts. You'd see something like this in Explorer:

-- Model
-- - Part
-- -- PointLight
-- - Part
-- - Part
-- - Part

This particular Model has four Parts as its Children. Each of those four Parts has that Model as its Parent. One of those Parts has a PointLight as a Child. Notice how the Model can have more than one Child, but each Part can only have one Parent? That is how scripts in Lua know which Instance is being referred to. It's sort of like a family tree, except instead of having two parents, an Instance only has one.

As another example, say you have a Script inside a Part:

-- Part
-- - Script

If you want to use that Script to change the Part, you first need to save a reference to it inside a variable, like this:

local Part = script.Parent

Part.BrickColor = BrickColor.new("White")

This is because scripts don't automatically know which Part you mean - you need to tell them which Part it is that you want to change.

If the Part had Children other than the Script, such as a PointLight or ParticleEmitter, if you wanted to change those, you would need to do a similar thing:

local Part = script.Parent
local PointLight = Part.PointLight

PointLight.Color = Color3.fromRGB(255, 0, 0) -- Red

Technically using variables (what comes after "local" in this case) isn't always needed, but it makes scripting easier.

Now, about game.Workspace. First, game is just a reference to the entire place as a whole - not only what you can directly see in Roblox Studio, but also things such as the Players that are in that game, etc. What you can directly see in Roblox Studio is what is inside Workspace. Workspace is also the eventual Parent of everything that you see.

You can use the fact that Workspace is the eventual Parent of everything that you see to refer to anything in the game using Workspace as a starting point.

-- Workspace
-- - Model
-- -- Part
-- - Baseplate
-- - Part

If you wanted to change the Part that is inside the Model, you would write:

local Part = game.Workspace.Model.Part

If you wanted to change the Baseplate, you would write:

local Baseplate = game.Workspace.Baseplate

Can you see how you would refer to the Part that isn't in the Model using the same method?

0
" First, game is just a reference to the entire place as a whole". No. game is a variable that references the DataModel class. User#19524 175 — 6y
0
Thanks for correcting me. NotInventedHere 158 — 6y

Answer this question