How do I get the name of my table?

So I have this basic table which contains all information of objects used in my game. But as you can see in the table, the name of each object has its own table, meaning that I cannot get the name of the object normally because it’s technically a table, not a string.

local PlaceSoundsFolder = script.Parent:WaitForChild("BuildSounds")

local BuildingParts = {
	["Tiny Engine"] = {
		Category = "Engine",
		Class = "Engine",
		IconId = "6219814488",
		Price = 250,
		Description = "A really small engine",
		BuildSound = PlaceSoundsFolder:WaitForChild("HardMetal"),
		Purchasable = true,
	},	
	["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"]) -- Returns table. Need it to return the name of the table instead

Some help would be greatly appreciated!

6 Likes

So you want it to like print out the Tiny Engine etc?

for Name, Table in pairs(BuildingParts) do
    print(Name)
end
2 Likes

yes, as shown here, I need it to print the table name instead of the contents of the table.

Try indexing? Maybe:

print(BuildingParts[1])

BuildingParts[1] would return nil because all the keys are strings and none are numbers.

1 Like

Is it possible to do this without the need of a loop?

2 Likes

This script will just print the number of the value ;-;

1 Like

As far as I know, no, but you can just insert the name inside the table

["Tiny Engine"] = {
        Name = "Tiny Engine", -- like this
		Category = "Engine",
		Class = "Engine",
		IconId = "6219814488",
		Price = 250,
		Description = "A really small engine",
		BuildSound = PlaceSoundsFolder:WaitForChild("HardMetal"),
		Purchasable = true,
	},	
1 Like

This is what I did originally, but I didn’t like having to worry about 2 strings that are the same each time I go and make a new table in the array.

If there is no other way to do this with out a loop. Then i’ll keep this as the solution for now.

1 Like

I think it’s best if you add a key which specifies the name inside that key.

1 Like

What’s your use case for this? Why don’t you want to use a for loop?

1 Like

I don’t really think you can get the table’s name. :roll_eyes:

1 Like

Yeah that’s what I really thought, just add a key named Name which contains the name of the nested table.

1 Like

You can just add a value at the table called name and print it. that’s it.

Well, I’m making a vehicle builder game which has a GUI with a list of parts that you can place, and each button within that list has its own function.

I mean, having a loop isn’t a problem, but for me, loops are just too big for small functions like this.

for i, PartInfo in pairs(BuildPartsDictionary) do -- That table from earlier
		if PartInfo.Category == PartCategory then
			local PartButton = PartListFrame.TemplateButton:Clone()
			PartButton.Name = PartInfo
			PartButton.PriceText.Text = "$" .. PartInfo.Price
			PartButton.LayoutOrder = PartInfo.Price
			PartButton.NameText.Text = PartInfo
			PartButton.Image = "http://www.roblox.com/asset/?id=" .. PartInfo.IconId
			PartButton.Parent = PartListFrame
			
			PartButton.MouseButton1Click:Connect(function()
				SelectModel(PartInfo) -- errors because the table is not a string.
			end)
		end
	end

Yoo, metatables is this what you were looking for?

local BuildingParts = {
	["Tiny Engine"] = {
		Category = "Engine",
		Class = "Engine",
		IconId = "6219814488",
		Price = 250,
		Description = "A really small engine",
		--BuildSound = PlaceSoundsFolder:WaitForChild("HardMetal"),
		Purchasable = true,
	},	
	["Large Engine"] = {
		Category = "Engine",
		Class = "Engine",
		IconId = "6219814488",
		Price = 10000,
		Description = "A really big engine",
		--BuildSound = PlaceSoundsFolder:WaitForChild("HardMetal"),
		Purchasable = true,
	},
}
local meta = {__tostring = function(table) return "Tiny Engine" end}
setmetatable(BuildingParts["Tiny Engine"],meta)

print(BuildingParts["Tiny Engine"]) -- Now returns Tiny Engine

But yeah tables are anonymous might be better to just add a name index to it unless you really want to print the table.

10 Likes

Are you trying the print the values in the table or trying to print the table’s name itself?

No, i’m trying to print the table’s name. Incase you haven’t noticed by the title of this thread.

Try this one.

local PlaceSoundsFolder = script.Parent:WaitForChild("BuildSounds")

local BuildingParts = {
	["Tiny Engine"] = {
        Name = "Tiny Engine",
		Category = "Engine",
		Class = "Engine",
		IconId = "6219814488",
		Price = 250,
		Description = "A really small engine",
		BuildSound = PlaceSoundsFolder:WaitForChild("HardMetal"),
		Purchasable = true,
	},	
	["Large Engine"] = {
        Name = "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"].Name)