I'm trying to make it so when I click a part, all of the parts inside a model change Transparency from 1 to 0, and also change CanCollide from false to true. Please help.
I tried this, but it didn't work:
1 | local children = game.Workspace.island 1 :GetChildren() |
2 | local button = script.Parent |
3 | function solid() |
4 | children.Transparency = 0 |
5 | children.CanCollide = true |
6 | end |
7 | button.ClickDetector.MouseClick:connect(solid) |
The result of GetChildren
is a table; A list containing Roblox objects, such as parts.
You cannot directly change a property from the table.
Instead, you must iterate -- or "browse" -- through the table using a "generic for" loop, like so:
01 | --[[ |
02 | Tables contain key-value pairs. |
03 |
04 | Each step of the loop, 'pairs' returns two values: |
05 | A table key and the value associated with it. |
06 |
07 | '_' and 'object' are variables. |
08 | The former is assigned the key, a "position" in the table, |
09 | and the latter is assigned the value associated to that key; |
10 | In our case, a child of "Island1". |
11 | ]] |
12 |
13 | for _, object in pairs (game.Workspace.Island 1 :GetChildren()) do |
14 | object.Transparency = 0 |
15 | object.CanCollide = true |
16 | end |
17 |
18 | -- As a convention, we generally name the key '_' if we do not use its value. |
I hope that answers your question; If it did, please make sure to mark my answer as accepted and optionally upvote. It helps both of us. If you still need anything, let me know.
1 | for i,v in pairs (game:GetService( "Workspace" ):FindFirstChild( "Island1" ):GetChildren()) do |
2 | v.Transparency = 0 ; |
3 | v.CanCollide = true ; |
4 | end |
just use a generic for loop to iterate through each element in the model at a a time. The i and v are variables which bind to each object's index, and each objects value.