I want to search for the children of all game files. Its something I did a year ago and cant remember the exact code. Im still very new to .lua. So far I have this which I knew was wrong and wont work but it should be close...
for i,v in pairs, #game.Lighting.GetChildren() do tostring(v) print(v) end;
Any help in cleaning that up into real code?
For future reference, please use Lua code formatting (the little Lua button above the text box on the editor.) when you post code. It makes it easier to see what's code and what's explanation.
It looks like you're trying to make a generic for
loop using a numeric for
style. This simply doesn't work.
First, pairs
is a function, and it takes a Table as an argument. GetChildren
returns a Table, but you have to call it as a method, using the :
operator.
tostring(v)
is unnecessary. print(v)
will automatically call tostring()
on v
, since v
is not a string.
Finally, and this is just a stylistic thing, unused return values (i
in your code) are typically given an underscore (_
) instead of a name, to indicate that they are not used.
for _, v in pairs(game.Lighting:GetChildren()) do print(v) end --Semicolons are not necessary in Lua 99% of the time.
Pairs takes a table as an arguement, such as the table of contents of Lighting. I think tostring is just for numbers or something. Use v.Name.
for _, v in pairs(game.Lighting:GetChildren()) do print(v.Name) end
take away the comma after in pairs
and it is :GetChildren()
i think you have to add parentheses around #game.Lighting:GetChildren()