Displaying an instance's complete reference

I have made a naive attempt to create a module that displays an instance’s complete reference starting from game as in game.workspace.part only if it is a descendant of the workspace, but my method returns an error telling me I’m indexing nil.

Here is my attempt :

  local CustomFunction = {}

      function CustomFunction.Index(item)
           if not item then
           return "item  parameter was not specified" end  
         	 local found 
          	 local ref = tostring(item)
          	 for  _, item in ipairs(workspace:GetDescendants()) do
            	       if item.Name == ref then
              	found = item
          if found.Parent == workspace then 
          return "game.workspace."..found.Name end
                 found = found.Parent    
                 ref = found.Name.."."..ref       
           until found.Parent == workspace
          return "game."..ref  end 

 return CustomFunction

What am I doing wrong, can anyone tell me please?

   local CustomFunctions = require(script.Parent.CustomFunctions)
   print(CustomFunctions.Index("part") )

   -----> attempt to index nil with 'Parent'

Edit : I find it mortifying to say I did not know what the :GetFullName function is

Although your intentions might be to learn how this can be accomplished, Roblox have a built-in function that returns the ancestry tree of an instance called GetFullName().



I know this has already been answered, but you could start at the instance and go upwards until you reach the datamodel.

function CustomFunction.Index(item)
	if (typeof(item) ~= "Instance") then
		return "invalid argument"
	if (item.Parent == nil) then
		return "nil"
	local str = item.Name
	while (item.Parent ~= game) do
		item = item.Parent
		str = item.Name.."."..str
	return str

The result should be identical to GetFullName()

1 Like