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

How to make a script that select all of a model's childrens?

Hi guys I tried to make a script that select all children of a model to make it easier to modify but could not find anything to start with, anybody have an idea?

2 comments

0

Define "select", as in the studio explorer kind of select or something else?

0

If you want to get everything in the model (models within models), I suggest using Instance:GetDescendants().

1 answer

You can use GetChildren():

local objects = game.Workspace:GetChildren()

You can also add a "#" to count the number of children the workspace has:

local NumberOfObjects = #game.Workspace:GetChildren()

However, if you want to do a function for every child, it's better to use a loop:

for i, v in pairs(game.Workspace:GetChildren()) do
	
end

In that case, the "i" is the number of loops that loop did and "v" is the child. Also, you don't need to add a "#" before game.Workspace:GetChildren(). For example, if you had a child named "Part" and another named "Part2" in the workspace (let's suppose there is no child named "camera" and "baseplate") and you wanted to change both of their transparency to 1:

for i, v in pairs(game.Workspace:GetChildren()) do
	v.Transparency = 1
end

However, sometimes, there is a child that you don't want to change. So you can use the "IsA" function:

for i, v in pairs(game.Workspace:GetChildren()) do
	if v:IsA("Part") then -- you insert the ClassName of the object you want to change here
		v.Transparency = 1
	end
end

You can also use an if statement to check v's Name:

for i, v in pairs(game.Workspace:GetChildren()) do
	if v.Name == "Part" or v.Name == "Part2" then
		v.Transparency = 1
	end
end

Sometimes, you will have like 15 children but there is just one that you don't want to change. So you have to add to the if statement all the other 14 children's Name. But you can also use the else function:

for i, v in pairs(game.Workspace:GetChildren()) do
	if v.Name == "Part3" then -- let's suppose we don't want to change "part3"
		-- the name of the part is Part3 so we don't change it
	    else -- if the part's name is not Part3
		v.Transparency = 1
	end
end

You may also heard of this:

for i, v in ipairs(game.Workspace:GetChildren()) do -- The "pairs" became an "ipairs"
	if v.Name == "Part" or v.Name == "Part2" then
		v.Transparency = 1
	end
end

The "ipairs" function does the same thing as the "pairs" function, but with "ipairs", when you encounter an object that is nil, it will stop the loop. For example:

local list = {"hamburger", "french fries", nil, "Hot Dog"}

for i, v in ipairs(list) do
	print(v)
end

-- Output

hamburger
french fries
-- the function stops

Hope this helped!

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