Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

4 comments

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.

0

Click the checkmark button

0

There is no checkmark button. Here, look at this screenshot: https://s22.postimg.io/dkrd5jae9/snap.png

0

I have never asked a question, so I don't know.

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.