How do I get the name of my table?

Wow, i haven’t thought of using meta tables.

Thank you sir!

No problem, however as you notice you will have to create a new metatable in order to name a table as how it was indexed which is painfull. To solve this we can automate it using more meta tables!

local BuildingParts = {}
local automate = {__newindex = function(table,index,value)
	local nameTable = {__tostring = function(table) return index end}
	setmetatable(value,nameTable)
	rawset(table,index,value)
end}
setmetatable(BuildingParts,automate)
BuildingParts["Tiny Engine"] = {
	Category = "Engine",
	Class = "Engine",
	IconId = "6219814488",
	Price = 250,
	Description = "A really small engine",
	--BuildSound = PlaceSoundsFolder:WaitForChild("HardMetal"),
	Purchasable = true,
}

BuildingParts["Large Engine"] = {
	Category = "Engine",
	Class = "Engine",
	IconId = "6219814488",
	Price = 10000,
	Description = "A really big engine",
	--BuildSound = PlaceSoundsFolder:WaitForChild("HardMetal"),
	Purchasable = true,
}
print(BuildingParts["Tiny Engine"])--Tiny Engine
print(BuildingParts["Tiny Engine"].Price)--250
print(BuildingParts["Large Engine"])--Large Engine
8 Likes

Wow. This is great to know.

Thank you!

2 Likes