Summoning Children in Code

Recently while I’m scripting I’ve been wanting to be able to summon Children in a script instead of naming out every child of a block. For example, if I want to make something transparent, I could type:

game.Workspace.Bookcase.Children.Transparency = 1

…instead of typing:

game.Workspace.Bookcase.Book.Transparency = 1
game.Workspace.Bookcase.Shelf.Transparency = 1
game.Workspace.Bookcase.Body.Transparency = 1
etc-

I don’t remember if you used to be able to do this, but what I am asking is if this is possible. All I see is ChildAdded and ChildRemoved.

Thanks.

GetChildren returns a table containing all children of an object. You can loop through a table using a for loop with pairs (or ipairs).

Putting these together, you might get something like this:

for _, child in pairs(workspace.BookCase:GetChildren()) do
     child.Transparency = 1
end

Edit: If you don’t know how to use pairs, here’s the wiki link for using standard library iterators: https://developer.roblox.com/en-us/articles/For-Loops#standard-library-iterators

4 Likes

you can use a for loop like this

for _,child in pairs(workspace.Bookcase:GetChildren())do
      child.Transparency = 1
end
1 Like

So if I have my function set up like this:

function onClicked()

wait(1)
for _, child in pairs(workspace.ThisBookCase:GetChildren()) do
child.Transparency = 1

end

should it work? Because it doesn’t seem to work for me. (When you click a part, it disappears.)

Are you trying to get the part you click on to disappear, or make it so that clicking on any part in the bookcase will make the entire thing disappear?

1 Like

What I have is a bookcase full of books, and when you click a certain book, then the bookcase should disappear.

Where do you connect your onClicked function? I think the issue is with how you’re detecting the click rather than the disappearing itself.

1 Like

I have it set up like so:

function onClicked()

wait(1)
for _, child in pairs(workspace.ThisBookCase:GetChildren()) do
 child.Transparency = 1

end

game.Workspace.ThisBookcase.Parttt.ClickDetector.MouseClick:Connect(onClicked)

Parttt being the book in the bookcase.

Huh, that should work fine. Are you getting any errors when you click the book? What kind of script is this (local/normal), and where is it located?

1 Like

To address your questions:

When I click the book, no errors appear.
This is all in a normal script.
The script is located inside Parttt aka the book which has the ClickDetector in it.

Whoops, looks like I missed something when reading your script over. I’m pretty sure it is erroring, because you’re missing an end. You need an end at the end of the for loop, and another one at the end of the function, and you only have one. For me, this errors like this:
20:51:32.533 - Workspace.ThisBookCase.Parttt.Script:9: Expected 'end' (to close 'function' at line 1), got <eof>

If it isn’t erroring for you, that’s really weird. Is the script disabled by any chance?

Edit: You also refer to the model as both ThisBookcase (in the connection) and ThisBookCase, (when using :GetChildren()) and I’m pretty sure that would error as well.

2 Likes