Get Name of Children of an intance

Hello!
When I use Intance:GetChildren(), I get the Table of children of the instance but do I have to use a for count loop and replace instance by instance name in a table or there are easier and faster solution?

Yep! I think you are right. It’s not really hard though.

You could make simple function like this:

local function GetNamesOfInstances(instancesTable)
    if not typeof(instancesTable) == "table" then return false end
    local namesTable = {}
    for _,v in pairs(instancesTable) do
        pcall(function()
            table.insert(v.Name, namesTable)
        end)
    end
    return namesTable
end

PS: I put that part in a pcall to avoid breaking the script.
If there are any issues with this function, please get back to us!

I hope I helped!
Have a great rest of your day.

You don’t need pcall here? Your script doesn’t work at all since it tries inserting a table to a string which doesn’t work for obvious reasons.

This won’t work how you expect either. not has higher precedence over ==. Just use type annotations though:

local function GetNamesOfInstances(instancesTable: { Instance? }): { string? }
    local namesTable = { }

    for _, instance in ipairs(instancesTable) do
        table.insert(namesTable, instance.Name)
    end
    return namesTable
end

Now your function won’t accept anything that isn’t an array of Instances.

1 Like

Oh I’m sorry! Thank you for correcting me. I messed up!

It is possible to use Instance:GetChildren instead your “instanceTable” table?

That is possible to do. It is probably even better than using the variable.


local namestable = {}
for i, v in pairs(instance:GetChildren()) do
      print(v.name)
      table.insert(namestable, v)
end