Editing Table name after creation?

So, I was working on an infinite dungeon type game, and to reduce client lag, I decided to try to make it so that dungeons were deleted as you passed through them (with the data saved in a script), and they would be reloaded when u passed back into them, in a way that there would only be one dungeon in the workspace at a time.
While trying to save the dungeon in a table, I wanted to have the table’s name be the name of the dungeon being saved for easier access. I was not completely sure how to go about doing this as

[part.Name] = {}
tostring(part.Name) = {}

did not work. After some head-scratching, I was told that I should use the function getfenv() to change the name of the table, however, it does not work all the time and occasionally returns the wrong table/dungeon. What exactly does getfenv() do, and how would I go about changing the table name?

My code for saving the dungeon:

function Deload(part)
	print("Deleting crypt #"..part.Name)
	if not CryptTable[part.Name] then
		local partTable = {
			Type = part:GetAttribute("Type"),
			Completed = part:GetAttribute("Completed"),
			Name = part.Name
		}
		part:Destroy()
		getfenv()[partTable] = part.Name
		CryptTable[#CryptTable + 1] = partTable
	end
end

you could do

NewTable = OldTable

OldTable = {}

This way you get a new table with your desired name that is exactly the old table, and then you clean the old table so that it doesnt affect performance

1 Like

You could do what your original solution was by instead using a dictionary to associate the part names with the data table.

local myDictionary = {}
myDictionary[part.Name] = myData
myData = nil
local myRecoveredData = myDictionary[part.Name]

The issue with that is that I want to make the name the name of a part (this is going through a loop so I can’t exactly specify).