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:
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.)