Need help with Get Children

So I need help with something
I dont know how to make it so the script goes to “Chest2” and gets the children, and everything with the name “one” it changes. So if its is named “one” it will do the script

this is because i dont want to name every single part a special name, and make a script for every single part. If that makes sense.

Do I use something called “GetChildren” I have tried, but I havent done it right

script.Parent.Touched:Connect(function(hit) 
	local BodyPart = hit.Parent:FindFirstChild("Chest2").One
	if BodyPart ~= nil then --Checks if the BodyPart exists or referenced correctly
		BodyPart.BrickColor = BrickColor.new("Sand blue") --changes the color
	end
end)
1 Like

Could you elaborate more, please? I don’t quite understand yet.

so in the script, where it says --find first child “Chest2”.one-- I want it to find the “Chest2”, and then get EVERYTHING named “one” (under the “chest2”) instead of just one part named “One” out of the like 4 I have

I dont want to make special names and special scripts idk if that makes sense

So you want to get the children of Chest2? If so, try GetDecendants(), it may work better for you.

script.Parent.Touched:Connect(function(hit) 
	local Chest2	= hit.Parent:FindFirstChild('Chest2')
	
	for i, v in pairs(Chest2:GetChildren()) do -- Loop through all the children inside Chest2
		if v.Name == 'One' then -- Check if the children named 'One'
			-- do something to the part
		end
	end
end)

I don’t believe looping through all the children would be necessary. Getting the descendants would get all the objects inside of Chest2, and create an array of them.

The thing is i only want the children named “one” under Chest2, how would i do that

script.Parent.Touched:Connect(function(hit)
    local BodyPart = hit.Parent.Chest2:GetChildren()
        for i = 1, #BodyPart do
        if i.Name == "One" then
            i.BrickColor = BrickColor.new("Sand blue")
        end
    end
end)

I belive this would work, let me know if otherwise. This basically loops through the BodyPart variable, and if the name of the child of the BodyPart variable is One, then it will change it to Sand blue.

That will not work. FindFirstChild only returns 1 object, or false.

This is why you need to use GetChildren as it will return a table of all child objects. Only then can use use a loop to check all children for their name.

Edit: Do not use GetChildren() directly after FindFirstChild, because if FindFirstChild returns nil, then GetChildren() will break the script.

12strings was partially correct. They forgot to check if Chest2 exists.

Here is the script you are looking for:

script.Parent.Touched:Connect(function(hit) 
	local Chest2 = hit.Parent:FindFirstChild('Chest2')
	if Chest2 then
		for i, v in pairs(Chest2:GetChildren()) do
			if v.Name == "One" and v:IsA("BasePart") then
				v.BrickColor = BrickColor.new("Sand blue")
			end
		end
	end
end)

I just updated it. (charrrrrrr 30)

Unfortunately your edit wont work either. “i” would be a number in this loop which means you can not do i.name. You would have to do: BodyPart[i].Name

Thank you this worked! ---------------

1 Like

I provided this in the other thread too, but here you go:

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("HumanoidRootPart") then
		local char = hit.Parent
		for i, part in pairs(char:GetDescendants()) do
			if part.Name == "one" then
				part.BrickColor = BrickColor.new("Sand blue")
			end
		end
	end
end

This will work for all body parts named “one” regardless of where they are positioned in the character model, not just parts named “one” which are inside a “Model” instance named “Chest2” which itself is inside the character model.

incorrect usage of WaitForChild. Also this was already solved 3 hours ago. He could easily change GetChildren with GetDescendants in the script I have provided in the solution if he wanted additional objects.

Should have been “FindFirstChild”, and it’s not incorrect at all, it’s a check to ensure that the “Touched” event was triggered resulting from the part being touched by a player’s character as opposed to a random part causing the event to fire. Also in your script you’re declaring a variable unnecessarily, you should perform the check first then declare the variable. if v.Name == "One" and v:IsA("BasePart") then This is also two unnecessary checks which could have just been one, as only “Part”/“MeshPart” instances which both share the “BrickColor” property will be named “One” or “one”.

You are wrong here. There is nothing wrong with the script in the solution. There was something wrong with your script before you changed it.

What you had previously was WaitForChild and that will yield, and does not make sense to use it in an if statement, because when it is no longer yielding, you are pretty much guaranteed that it exists, otherwise the script will print a warning after the timeout period.

This is just preference and totally ok to do.

Those are correct because you do not want to assume “One” is a basepart object and that it is safe to assume you can use BrickColor, because in the event that it is not a basepart, setting a brickcolor will cause an error.

1 Like

You wrote 2 paragraphs about a typo? After reading the first line I’m going to ignore the rest.

Under what natural circumstance would a player happen to have an instance named “one” in their list of descendants without otherwise external intervention?

local Chest2 = hit.Parent:FindFirstChild('Chest2')
if Chest2 then

You’re declaring then making a check, as opposed to making the check first then declaring, which is a waste of a line of executed code.

It is a local variable, which gets discarded from memory after the script runs that section of code. the resource being wasted as you say, is minimal at best. Also it is in variable form for easy access when using it in the loop so we dont have to go and reference it again.

Also it’s not a waste of a line of code because you would need to reference it at some point:

local Chest2 = hit.Parent:FindFirstChild('Chest2')
if Chest2 then
	for i, v in pairs(Chest2:GetChildren()) do

vs

if hit.Parent:FindFirstChild('Chest2') then
	local Chest2 = hit.Parent.Chest2
	for i, v in pairs(Chest2:GetChildren()) do

I mean if we skip putting it in a variable then sure:

if hit.Parent:FindFirstChild('Chest2') then
	for i, v in pairs(hit.Parent.Chest2:GetChildren()) do

but again that’s all just down to preference.

1 Like

What’s with the irrelevant off-topic response? I stated it would be better to perform the 2 lines in reverse order.