Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Getting the name of a table (Metatable?)

local Ups = {
 Body = {
	
	{"Still",
		{"Still1",15,100,false,"Its a still part",
			{DrainR = 1,Part = 2}},
			.
			.
			.
}

Im trying to script a tech system by using a lot of tables. I usually iterate through them multiple times like

for j,l in pairs(Ups) do
	for k,v in pairs(Ups[j]) do
	.
	.
	.
--And so on. Its basically Inception...

It works fine (Maybe its inefficient, i wanted to avoid making scripts for each invidual button and manually placing the buttons) but i have a problem where i have to get the 'Body' from the table, but its not actually a member so i cant figure out how. Basicly what i want is for print(Ups[ j or 1]) to return "Body". Because i have a seperate table for the amounts of parts the player has on what they're building and what the limit for parts is. (Im using the same table for both Tech System and Building System)

	local roverlimit = {
		Wheels = 0,Internal = 0,External = 0,Body= 1}
			
	local roveramount = {
		Wheels = 0,Internal = 0,External = 0,Body = 0}

I need to get the Ups[j] so i can look at what they are in roverlimit and roveramount. So i can do roverlimit[Ups[j]]

1 answer

✓ Accepted answer

I think you're misunderstanding what the return variables from pairs and ipairs are.

In your code, l == Ups[j] l is the Value, j is the Key. Since your key is the string, "Body", j holds the string "Body" for the first iteration of the outer loop.

local Table = {
	Test = {yoloswag = "lol"};
	Test2 = {See = "This is how they work.", Its = "moreValues lol"};
}

for j, l in pairs(Table) do
	print("Searching sub-table " .. j)
	for k, v in pairs(l) do
		print(k .. " = " .. v)
	end
end

8 comments

1

Also, as a bit of explanation on metatables, nested tables are not metatables. Metatables manipulate the behavior of specific Tables, nested Tables are simply members.

0

Thanks, i actually could have noticed that but i got confused with all the inception D: It still doesnt work for some reason. I changed print(Ups[j]) to print(j) and it gives an error on the Call Stack and takes me to that line....

0

Can you take an upload a screenshot of the error, or paste the text of it in its entirety to your question?

0

http://s9.postimg.org/72ppcpytb/Capture.jpg As you can see, it doesnt print either.

0

There's no error text in that screenshot. What's the Script Analysis look like?

0

There's nothing about it

0

It suddenly decided to work..

0

Well okay then.

This discussion is preserved as it was. New answers, comments, and votes are closed.