How do I do it? for example search the whole workspace existing with the same properties? and it searches through models?
✓ ANSWER ACCEPTED
How to search the workspace?
1 answer
✓ Accepted answer
You need a recursive function -- that is, a function that calls itself.
function search(where, what, results)
--where: the desired model
--what: a function that returns true if it's what you're looking for
--results: you don't need to provide this; it's the table of results
results = results or {}
if what(where) then table.insert(results, where) end
local ch = where:GetChildren()
for i = 1, #ch do
search(ch[i], what, results)
end
return results
end
function printResults(results)
for i = 1, #results do
print(i, results[i])
end
end
--example of usage:
local results = search(workspace, function(obj) return obj.Name == "Special" and obj:IsA("BasePart") end)
printResults(results)
Inline explanations:
function search(where, what, results)
results = results or {} --initialize 'results' to an empty table if it has no value
if what(where) then table.insert(results, where) end --if the custom function 'what' specifies that 'where' (some object in the workspace) is what we're looking for, add it to the table of results
local ch = where:GetChildren() --get the list of children of 'where'
for i = 1, #ch do
search(ch[i], what, results) --recurse on each child
end
return results --return the table we collected
end
function printResults(results) --this is just a function to print out the results we found
for i = 1, #results do
print(i, results[i])
end
end
--Now, in the usage example, we have a call to "search":
search(workspace, function(obj) return obj.Name == "Special" and obj:IsA("BasePart") end)
--Broken down:
search(
workspace, --where to search
function(obj) --'obj' is something in the workspace that 'search' is considering
return obj.Name == "Special" and obj:IsA("BasePart") --this means "return true if the name of the object is 'Special' or if the object is of the class "BasePart". You can change this line to serve your needs.
end)
3 comments
0
Couldn't he use the FindFirstChild's second argument, that searches all descendants of what it's looking though? http://wiki.roblox.com/index.php?title=API:Class/Instance/FindFirstChild
0
@OldPalHappy That only if he's looking for a single object with a specific name. If you want 2+ objects, or are looking for something with a specific property, FindFirstChild won't help.
0
I understand. You're right. :3
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
What do you mean find all instances of the same type or name ?
Be specific. What are you searching for?