How to get an instance's index in explorer (with script)?

Hello devs!

I have a question, how can I get an instance’s index in explorer?
Example:
image
The Script’s index is 1, the strVal’s index is 2, …

Please let me know if there’s any solution!

1 Like

This might not be the most performant way to get the index, but I stumbled upon it earlier. GetChildren() automatically gets the indexed pairs of the index and the instance in an array. So, you can simply use table.find(parent:GetChildren(),instance), which will return the index. Just tested it and it works.

2 Likes

Hmm, it doesn’t seems to work, the indexes are messed up:
image
image

I think that the order of the children in studio is based on the timestamp for that instance was created, at least from my experience. It seems like there aren’t actually any “indexes” for an instance’s children in roblox though, in that the children aren’t stored in an array, but a key-value pair dictionary. For example, if you do testpart[1], nothing shows up, as it would in an array (unless you name a child “1”), but if you do testpart[“Script”], script will show up. So basically, there is no “index”, just a key, and the key is the name of the object itself.

1 Like

Why would you need to get the order of appearance in the explorer? Anything relying on this behavior is probably not the best idea. There might be a way to get around the problem you are facing with a different approach.

1 Like

I’m developing a plugin and I need to get the index in the explorer for a main feature.

What exactly is the feature you’re working on? As I mentioned, there might be a better way to solve your issue.

It’s a command bar plugin and I want to make so you can navigate trough the children of an instance with commands.

From what I can understand of this, I don’t see why you can’t index the children by name. I might be misunderstanding the purpose of your plugin though.

There’s two commands now, up and down.
Example:
image
if I run the down command it should select “boolVal”
if I run the up command it should select “Script”

But I can’t do this because I need to get the index of all instance’s first.

You can making a function which loops through the objects parent, check if its the given object then return its index.

local function getindex(object)
local parent = object.Parent

for index, item in ipairs(parent:GetChildren()) do
if item ~= object then
continue
end

return index
end
end

print(getindex(workspace.testpart))

I couldn’t come up with any other ideas, I don’t know if I should use pairs or ipairs.

why not use ipairs after they all loaded in???

This is what I’m doing now, and it gives random numbers back.

ipairs doesn’t seem to loop trough instances as they are in the explorer.

local function getindex(object)
    local parent = object.Parent

    for index, item in ipairs(parent:GetChildren()) do
        if item ~= object then
            continue
        end

        return index
    end
end

print(getindex(workspace.testpart))

added spaces so it looks better

if you use :GetChildren() you can do ipairs, wdym???

It doesn’t give the instances back in the same order as they are in the explorer.

While I understand how this might be useful, you’re replacing the usage of a mouse. Providing alternate control schemes isn’t always a bad thing, but this is such a simple feature it might be worth thinking of a different way to implement your ideas. The ordering of the explorer, like discussed on the thread, doesn’t match up with any indexing exposed to developers - and the order it shows now is always subject to change and probably shouldn’t be relied on to be consistent.

you must have done it before they were all loaded in

They are all loaded in because I’m making a plugin.