Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to find all parts of a certain name?

Asked by 10 years ago

I want to insert a script into all parts that are a certain name, eg. "PutScriptHere". I know how I would get all the children of the Workspace...

game.Workspace:GetChildren()

But, how would I actually look through them, like, for example, I would a table -

for i = 1, #table do
print[i]
end

I can do the checking the name myself if I have the code to look through the parts. I'm assuming I would do that by doing -

if part.Name == "PutScriptHere" and part.ClassName == "Part" then
script.ThingyScript:Clone(part)
end

Can anyone help? c:

1 answer

Log in to vote
1
Answered by
2eggnog 981 Moderation Voter
10 years ago

Here's the full script you're looking for.

for i,part in ipairs(Workspace:GetChildren()) do
if part.Name == "PutScriptHere" and part.ClassName == "Part" then
script.ThingyScript:Clone().Parent = part
end
end

For more info on this solution, visit http://wiki.roblox.com/index.php?title=Ipairs#ipairs

Below is alternate solution using a regular loop. They both do the same thing, but the previous script looks nicer and is easier to type out.

parts = Workspace:GetChildren()
for i=1,#parts do
if parts[i].Name == "PutScriptHere" and parts[i].ClassName == "Part" then
script.ThingyScript:Clone().Parent = parts[i]
end
end

(As a side note, pairs() and ipairs() do very similar things. pairs() is used when you have tables with non-integer keys, while ipairs() only supports integer keys. GetChildren() always returns a table with integer keys only, so it's best to use ipairs() in that situation.)

0
Thank you! Works a treat :) DevJackB 55 — 10y
0
You're welcome. If you found it helpful, I hope you wouldn't mind marking my answer as accepted. :) 2eggnog 981 — 10y
Ad

Answer this question