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 get all children with a certain name?

Hi there, I am trying to get all children parts named "Straight" and all named "Turn" and set the collisions of them.

This is what I tried but its not working

*Note that "Part" is a detector used to trigger the script and the model folder for the parts im wanting to change the collision script is the parent of "Part", eg. Part.Parent

Hope that makes sense :)

local prts = Part.Parent:GetChildren()
for i = 1, #prts do
	if prts.Name == "Straight" then
		prts[i].Transparency = 1
		prts[i].CanCollide = false
	elseif prts.Name == "Turn" then
		prts[i].Transparency = 0
		prts[i].CanCollide = true

3 comments

0

This isn't working because you're checking the table's name (prts.Name). Tables do not have a name property. Instead, you should do prts[i].Name.

0

ah thanks ill give that a go

0

change 'if prts.Name' to 'if parts[i].Name

1 answer

✓ Accepted answer

One way i would do this is with a generic for loop, which is different from what you're doing, which is a numeric for loop.

Generic

for i,v in pairs(table) do
	print(i,v)
end

Numeric

for i = 1,54,2 do
	print(i)
end

While a numeric for loop can work for the purpose you're trying to achieve, it isn't really the prefered method, as most could argue that :

for i,v in pairs(Thing:GetChildren()) do
	print(i,v.Name)
end

is more efficient than

local tbl =Thing:GetChildren() 
for i = 1,#tbl do
	print(tbl[i])
end

Also, the problem with your script is that a table,prts does not have a Name property, try using prts[i] or try using a generic for loop:

local prts = Part.Parent:GetChildren()
for _,v in next,prts do
	if v.Name == "Straight" then
		v.Transparency = 1
		v.CanCollide = false
	elseif v.Name == "Turn" then
		v.Transparency = 0
		v.CanCollide = true
	end
end

FYI, the _ is used as a dummy variable if you don't need it to be used.

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