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

How to change properties of all the parts inside a model?

Asked by 7 years ago
Edited 7 years ago

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:

local children = game.Workspace.island1:GetChildren() 
local button = script.Parent 
function solid() 
    children.Transparency = 0 
    children.CanCollide = true 
end
button.ClickDetector.MouseClick:connect(solid)

2 answers

Log in to vote
4
Answered by
Link150 1355 Badge of Merit Moderation Voter
7 years ago
Edited 7 years ago

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:

--[[
    Tables contain key-value pairs.

    Each step of the loop, 'pairs' returns two values:
    A table key and the value associated with it.

    '_' and 'object' are variables.
    The former is assigned the key, a "position" in the table,
    and the latter is assigned the value associated to that key;
    In our case, a child of "Island1".
]]

for _, object in pairs(game.Workspace.Island1:GetChildren()) do
    object.Transparency = 0
    object.CanCollide = true
end

-- 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.

0
Thank you so much, my script is finally working. I would upvote your answer if I could, but I don't have 5 reputation points. Also, how do I mark your answer as accepted? I'm very sorry for my inexperience, this is my first time using this website. Rodmane234 85 — 7y
0
Click the checkmark button starlebVerse 685 — 7y
0
There is no checkmark button. Here, look at this screenshot: https://s22.postimg.io/dkrd5jae9/snap.png Rodmane234 85 — 7y
0
I have never asked a question, so I don't know. Link150 1355 — 7y
Ad
Log in to vote
0
Answered by
movsb 242 Moderation Voter
7 years ago
for i,v in pairs(game:GetService("Workspace"):FindFirstChild("Island1"):GetChildren()) do
    v.Transparency = 0;
    v.CanCollide = true;
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.

Answer this question