Find Object in Table

So now that I know about table.find(), I’ve noticed that I can’t simply check if an object in a table exists via its name.

Example:

print(table.find(workspace:GetChildren(),'Part'))

Output: nil
Is there a way I can use table.find to work in this situation, or do I just need to iterate through and find it “manually”?

Thanks

Instance:GetChildren returns a table of instances, not strings. So you will have to do a loop.

1 Like

Just to confirm, there’s no way to use string.find in this case? No way to check parts by name?

1 Like

Why string.find?

Instance:GetChildren doesn’t return a table of names, but references to the instances themselves.

print(Instance.new("Part") == "Part") -- false

Sorry, I meant table.find. I was just trying to make sure that there’s absolutely no way to use table.find in that way. I understand that it returns objects and not strings, but sometimes there seems to be ways to request a search via object names instead. Either way, that doesn’t seem to be the case in this situation.

Thanks

3 Likes

Absolutely no way, because GetChildren returns references to objects. Your table would look like this:

{
    workspace.Part1,
    workspace.Part2,
    ...
}

Can’t use table.find on something like that. Not even if you have the string set to the path because then the value types aren’t equivalent. Have to use a loop.

If you want to find only parts by a certain name then you could do it this way.

local function findPart(name)
    local instance = workspace:FindFirstChild(name)

    if instance:IsA("Part") then
        return instance
    end

    return nil
end

local stopSignPart = findPart("StopSign")
if stopSignPart then
    print("StopSign found.")
else
    print("Not found, returned nil")
end