How to get the path of an Instance(STRING)

This is a tutorial on how to get the path of an Instance.

this is what the code would look like:

function getPath(instance)
	local path = ""

	local ins = instance

	repeat ins = ins["Parent"] path = path.."."..ins.Name until ins == workspace
	
	return path
end

If you would like to go through how this is made and understand it, keep reading :slight_smile:

In order to start, we need a base variable to constantly join to.

local path = ""

simple. Now we need the instance given.

local path = ""

local ins = [instance given]

Now it gets more complicated.

The way this will work is all because of these (“…”, and [string])

[string] will allow you to get the child or property of an instance(I believe this works for tables as well but I may be wrong)

… will allow us to join two strings together.

print("Hello".." world!")

we can use this to our advantage by constantly joining strings.

first, use a repeat loop.
Inside of it, add ins = ins and add a ["Parent"] to get the Parent property from the ins object. This will constantly get the parent of the new instance that has overridden ins.

local path = ""

local ins = instance

repeat ins = ins["Parent"] until

now after that, set the path variable and join it with the name of the ins variable that we just updated, but put ..".".. inbetween so it has the proper full stop in between so it isnt all squished together.

local path = ""

local ins = instance

repeat ins = ins["Parent"]  path = path.."."..ins.Name until

now, we finish off the repeat until loop by adding the parameters ins == workspace, causing the loop to end once the script has reached workspace.

local path = ""

local ins = instance

repeat ins = ins["Parent"]  path = path.."."..ins.Name until ins == workspace

if you’d like, you can top this off into a function and return the path variable so you get the string directly after.

seat = workspace.Car.Base.VehicleSeat


function getPath(instance)
	local path = ""

	local ins = instance

	repeat ins = ins["Parent"] path = path.."."..ins.Name until ins == workspace
	
	return path
end

print(getPath(seat))

I hope this helped anyone who needed it! :slight_smile: thank you for reading.

EDIT: i noticed for some reason devforum highlights path, I can assure you in studio it is not marked as something else.

5 Likes

This won’t work if the item isn’t a descendant of Workspace. Why don’t you just use instance:GetFullName()!? It’s already implemented and works much better.

11 Likes

wow. I had 0 clue thats what this did :sob: i wasted so much time huh

how did you waste so much time on a 4 line script

1 Like

It’s okay, the main point is that you learned. You could’ve continued using the same function always and not the actual well-made function, you should be happy about that :).

1 Like

it was exaggeration. I didn’t literally mean it like I spent weeks writing that.