How Can I refrence a duplicated Instance locally

Hey Developers! This might seem like a simple issue and I feel like I should know how to solve it. How can I get an Instance that is duplicated. For example I have this script:

local ScreenGui = game.StarterGui.TestGui.MainFrame

How can I refrence the Duplicated one with Only knowing it’s StarterGui Location?

Aka this:

local WhatINeedToGet = game.Players.LocalPlayer.PlayerGui.TestGui.MainFrame

And how can I make sure it is the correct instance? (if per say, there is many frames within that Gui, or Gui that share the same name, but are different Instances)

Any Help is much Appreciated!

1 Like

You can create something comparable to a relative path to do this:

--TODO double check path is really unique
function module.GetUniqueRelativePath(pathTo : Instance, from : Model) : string
	if not pathTo:IsDescendantOf(from) then
		error("Can't find relative path, pathTo Instance is not a descendant of 'from'")

	local path = pathTo.Name
	local parent = pathTo.Parent

	while parent ~= from do
		path = parent.Name ..".".. path

		parent = parent.Parent

	return path

function module.ResolveRelativePath(path : string, base : Instance) : Instance
	local names = string.split(path, ".")

	for _, pname in names do
		base = base[pname]
		if base == nil then
			error("Invalid relative path. Base instance: " ..tostring(base.Name).. " Path string: " ..tostring(path))

	return base
1 Like

Awesome, Thank you so much for your help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.