Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Getting the name of a table (Metatable?)

Asked by
Turgon 80
10 years ago
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

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 years ago

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
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. adark 5487 — 10y
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.... Turgon 80 — 10y
0
Can you take an upload a screenshot of the error, or paste the text of it in its entirety to your question? adark 5487 — 10y
0
http://s9.postimg.org/72ppcpytb/Capture.jpg As you can see, it doesnt print either. Turgon 80 — 10y
View all comments (4 more)
0
There's no error text in that screenshot. What's the Script Analysis look like? adark 5487 — 10y
0
There's nothing about it Turgon 80 — 10y
0
It suddenly decided to work.. Turgon 80 — 10y
0
Well okay then. adark 5487 — 10y
Ad

Answer this question