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

How do you select all of the children in a model and modify them?

So I have a tool that once you equip it, all of the children in a model(invis) become visible, and then the other way around. However, although right now it is only 3 parts, it will expand. Is there a way to make this more efficient rather than how I started doing this?

Thank you here is the (not full) code:

local function onEquip() -- modify to select all children in parent invis?
	workspace.Invis.Part1.Transparency = 0
	workspace.Invis.Part2.Transparency = 0
	workspace.Invis.Part3.Transparency = 0
end
 
local function onUnequip()
	workspace.Invis.Part1.Transparency = 1
	workspace.Invis.Part2.Transparency = 1
	workspace.Invis.Part3.Transparency = 1
end

1 answer

✓ Accepted answer

Let's use tables and loops!

So, let's assume everything inside workspace.Invis are the parts we want, with no extra script, ect. We can use GetChildren() to get everything inside of the part, model, folder, ect, and use a integrated for loop to go through everything, changing what we want.

First, lets get everything in workspace.Invis with GetChildren,

local parts = workspace.Invis:GetChildren()

Now we have to loop through to get all the parts,

local parts = workspace.Invis:GetChildren()

for i,v in pairs(parts) do
	print(v.Name)
end
This will print the names of all the parts/objects in the table, "parts".

Now we have to simple change the Transparency property of the parts.

local parts = workspace.Invis:GetChildren()

for i,v in pairs(parts) do
	v.Transparency = 1
end

If however not everything in workspace.Invis are parts, we can check for this with the IsA() function. This is only necessary if you have things in workspace.Invis that are not parts.

local parts = workspace.Invis:GetChildren()

for i,v in pairs(parts) do
	if v:IsA("Part") than
		v.Transparency = 1
	end
end
You would use an and operator and check for a Union if you are using them.

Adding this information to the functions,

Simple enough. Just place this code in the functions. Example,

local parts = workspace.Invis:GetChildren()

local function onEquip()
	for i,v in pairs(parts) do
		v.Transparency = 0
	end
end

local function onUnequip()
    for i,v in pairs(parts) do
		v.Transparency = 1
	end
end

Let me know if you have any issues.

Good Luck!

1 comment

1

You would be the one to answer my question

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