of     1   

za_pa
#63450234Sunday, February 26, 2012 1:25 AM GMT

I have started to script, and I would like to see how getting children would work. The explanation on the wiki only says: "Getting Children It is possible to use "for" to get to children of anything, e.g.,:" then after that, a bunch of code, and nothing else. Here is the code they gave on the roblox wiki. a = game.Workspace:GetChildren() for i = 1, #a do if (a[i].className == "Part") then a[i].Reflectance = 1 else return end end Can someone explain how this works?
slayer219
#63450448Sunday, February 26, 2012 1:29 AM GMT

The roblox wiki fails again, surprise surprise. When using the GetChildren() method, you want to use a 'for do' statement. This loops through all the items specified looking for the ones that meet the criteria you have set. Let's break it down: a = game.Workspace:GetChildren() -- converts the children of Workspace to a table for i = 1, #a do -- searches through the table if (a[i].className == "Part") then -- the criteria, it MUST be a part a[i].Reflectance = 1 --changes the reflectance of all parts else return end -- if it's not a part, the script ends end -- forgot an end. end
UFAIL2
#63450580Sunday, February 26, 2012 1:31 AM GMT

a = game.Workspace:GetChildren() -- this finds every object in Workspace for i = 1, #a do -- goes for how many objects are in Workspace if (a[i].className == "Part") then -- if the object is a part then a[i].Reflectance = 1 -- the object's Reflectance property is change to 1. else return end end -- else stop script ~When life gives you lemons, you say 'I ain't even mad'.~
slayer219
#63450660Sunday, February 26, 2012 1:33 AM GMT

By the way, using 'else return end' just confuses people. Use else return false instead.
ThomasChabot
#63450662Sunday, February 26, 2012 1:33 AM GMT

It returns a table of everything located inside of whatever object you use :GetChildren() with. For example, if you have the following in Workspace: Workspace  > Base  > Camera Then using Workspace:GetChildren() would return a table with the values inside of it being Base and Camera. The for statement provided in the Wiki would make every part in Workspace be completely reflective until it reaches something that isn't a part, which stops it from searching. Tables are also fairly advanced, so I'm not sure if you would have used them or not yet. However, if you haven't, a simple explanation of them is that they return several values (In this case, objects), and using table[number] returns whatever value is at that point. For example, when the table has the following values: "H" "E" "L" "L" "O" print(table[2]) would print the second value, or "E". PM me if you have any more questions, or if you didn't understand this, and I'll explain it again. :D
ThomasChabot
#63450769Sunday, February 26, 2012 1:35 AM GMT

@Slayer, the 'else return end' is like saying: else   return -- The block of code inside the else statement end -- Ending the if statement So it's not returning anything, but it's exiting the for loop.
slayer219
#63450819Sunday, February 26, 2012 1:36 AM GMT

@L2000: Do you even know what return means?

    of     1