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
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! 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.