How to get all ancestors of an instance

How would i do this? I thought of GetAncestors but that doesn’t exist.
I’m making an antivirus plugin. I want the path of the virus to be specified. Also i can’t use a for loop appearently?
code:
image
permission error:
image

1 Like

I’m still a little confused as to what you’re trying to do, but one option to loop through all of an Instance’s ancestors is to use a repeat until loop.

Here’s an example:
I have this format in my Explorer window, where Workspace > Folder > Part > Script

I can set a variable as the Instance, and constantly set the variable as the variable’s parent until the variable refers to game, which is an ancestor of all existing Instances.

local current = script --The instance you're examining

repeat
	current = current.Parent --Set current to its own parent
	
	print(current) --Print out current
	--[[
	Some logic here
	
	e.g 
	if current.Name == "Workspace" then
		print("Workspace is an ancestor of the instance")
		break
	end
	]]
	
until --Keep repeating the code between 'repeat' and 'until' UNTIl current == game
	current == game

This script essentially prints out all of the ancestors of a given Instance. I also commented in some code for potential conditional logic.

Hopefully this answers your question. If not, let me know, and I’ll get back to you.

2 Likes

i’m turning all of that into a table, how would i get the index? (as it’s obviously not yet created. i’m making the path easy to read.
I get the ancestors by making a string value, making a repeat loop and parenting the string to the parent of the virus and setting a got values to true. It also adds the parent to the value of the string which adds it to the table.

I don’t know what this means.

Let me ensure that I understand you correctly. Let’s say the virus is in Workspace > Folder > Part > Virus. Your plan is to create a StringValue which is a child of Part and apply the repeat loop to that StringValue? If so, this would be the same as applying the loop to the Virus instance itself. Nonetheless, this still works.

You also plan to create a table of the virus’s ancestors, which I think is a good idea. To do this it would probably be a simple array format, where the keys are integers.

In the little example above, the table would be:

{
  [1] = Part,
  [2] = Folder,
  [3] = Workspace,
  [4] = Baseplate -- For some reason, an instance called Baseplate is shown when you type Workspace.Parent. I have no idea why but it shouldn't have an effect on your code.
}

You can add to your table by keeping track of an integer index or (the preferred method) using table.insert().

1 Like

I used table.insert which i don’t think is a big difference. I’m just wondering how I’d get those values out. I need to get each one because i want the file path to be something like this: game.Workspace.Folder.BlahBlahBlah.
You’ve been a life saver so far.

1 Like

table.insert() is correct. That was a mistake on my end.

You should be able to loop backwards through the script using a for loop. You can then concatenate the names of the instances with a “.” sign. Excluding the weird “Baseplate” stuff and adding your own “game” to the beginning, this should create a string of “game.Workspace.Folder.BlahBlahBlah”.

I don’t know what your plan for this is, though. If you’re trying to use this address in code, you’d have to take this string and turn it into Luau. To do this, I suggest you use something like loadstring().

1 Like

I got class now so I gotta dip for a bit I’ll be back later though

2 Likes

I want to get the contents of the table once the script adds them. It’ll be something like
print(table[1]…"."…table[2])
And so on. The problem is however i don’t know how many arrays the script will create. I need to somehow get the contents, put them in numerical order and add dots between each one. May have bit more than i can chew.

1 Like

Do you think you could clarify what you are saying? I don’t understand how you would have multiple arrays.

1 Like

I’m sorry for not being clear, but the table contains each ancestor of the virus. So when the string gets parented to a new parent, it’ll add it to the table. I won’t know how many arrays it will create because there can be a big number of parents. I’m wondering how I’d get each array and put them in order and add dots so it’ll look like
game.Workspace.BlahBlah.Injector

1 Like

Ohhhh I think I get it. So the StringValue will just serve as a temporary iterator?

In other words, the final table should look something like this? And you’d want to work on concatenating the strings as well, I would assume.

{
  {
    [1] = "Part",
    [2] = "Folder",
    [3] = "Workspace",
    [4] = "game",
  },
  {
    [1] = "Folder",
    [2] = "Workspace",
    [3] = "game",
  },
  {
    [1] = "Workspace",
    [2] = "game",
  },
  {
    [1] = "game",
  }
}
2 Likes

Yes! You’ve got it! Charsssssss

1 Like

So you want a script that takes one of those above arrays, and creates an array that contains all of the ancestor addresses? Like this?

local ancestorTable = 
	{
		{
			[1] = "Part",
			[2] = "Folder",
			[3] = "Workspace",
			[4] = "game",
		},
		{
			[1] = "Folder",
			[2] = "Workspace",
			[3] = "game",
		},
		{
			[1] = "Workspace",
			[2] = "game",
		},
		{
			[1] = "game",
		}
	}

local addressStrings = {}

for i, address in pairs (ancestorTable) do
	local s = ""
	for j = #address, 1, -1 do
		s = s .. address[j]
		s = s .. "."
	end
	s = string.sub(s, 1, string.len(s)-1)
	table.insert(addressStrings, s)
end

print(addressStrings)

image

I still feel like I’m not 100% understanding still.

2 Likes

Thanks! I’m going to mark it as a solution as soon as I test it. One last question, can you do IsA in plugins? It says i don’t have permission and it’s my if statement checking if the virus is a script for no false-positives.
I still don’t understand tables. I don’t know why. Especially for loops with numbers.

1 Like

Unsure of the context in which you’re using it, but I have heard of some issues regarding IsA() in similar cases to yours.
Is this the same error you’re getting? Maybe this post will help.

Yeah… they can be quite challenging! It’s just something that you’ll have to practice over and over until you master it. You got it though!

2 Likes

Close to my error, but i have permission 5, not 2.
Oh. Roblox doesn’t let you use IsA. I’ll have to find a bypass to read the script code.

2 Likes

hey i’m really late but uhhh
image

1 Like

What’s the issue? Are you asking why the ID’s are printed out like that?

1 Like

Well i didn’t want the ids printed. I wanted the contents. I really suck at tables huh.

1 Like

Go to the output log, click the 3 dots and turn off log mode.

1 Like