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

Is it possible to get a name of a variable in a table?

Asked by 4 years ago

I want to get the name of a variable in a table. Here is the script so far

ItemID = {"Nothing", "AHH6", "AK-47", "AK-47S", "AN-94", "AN-94M", "ATHK", "AWP", "DDT Rifle", "M1911", "M1911-M", "M92", "M92-S", "TH3"} --Table
IIDV = script.Parent.ItemID --IntValue
ItemName = script.Parent.ItemName --StringValue

I want to get the name of the variable, not the index value, to put in the ItemName string value.

3 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

I assume u want a list of vars in a list.

Here is an example-

local variables = {
    var1 = 'nothing'; -- Make sure u add the ; from every var u put
    var2 = 'AK-47';
}

print(variables.var1)

This will output 'nothing'

What you could also do in your case is to do the same thing but add what you want to display.

Here is an example-

local itemName = {
    item1Name = 'AK47';
    item1Name = 'nothing;
}

local itemNum = {
    item1Num = 1;
    item1Name = 2;
}

IIDV = itemNum.item1Num -- ITEM 1 NUMBER
ItemName = itemName.item1Name -- ITEM 1 NAME

Ad
Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
4 years ago

Not sure if i understand you correctly but i think i understand what you're asking. You want to get the value of your table and not the index?

If so try this:

local ItemID = {"Nothing", "AHH6", "AK-47", "AK-47S", "AN-94", "AN-94M", "ATHK", "AWP", "DDT Rifle", "M1911", "M1911-M", "M92", "M92-S", "TH3"} --Table

local ID_num = 2 -- change this to what index you want but beware if you set this to a value outside the table ie the nhumber of objects it will break the script!

local IIDV = script.Parent.ItemID --IntValue

local ItemName = ItemID[ID_num] -- AHH6! -- see above!
--local ItemName = script.Parent.ItemName --StringValue

If this is not what your asking could you ask a little clearer? so we can help you :)

Log in to vote
0
Answered by 4 years ago

Got bored decided to make an answer...

local items = {"hello","test1"}
for _, v in pairs(items) do
    if v == "hello" then
        print('congrats this is way too easy, found "hello"') -- it got hello
    end
end

print(items[2]) -- test1


-- solution if you didn't understand above:

ItemID = {"Nothing", "AHH6", "AK-47", "AK-47S", "AN-94", "AN-94M", "ATHK", "AWP", "DDT Rifle", "M1911", "M1911-M", "M92", "M92-S", "TH3"} --Table
IIDV = script.Parent.ItemID --IntValue
ItemName = script.Parent.ItemName --StringValue

for _, v in pairs(ItemID) do
    if v == ItemName.Value then
        print('got the name.')
    end
end

Basically loop. All the other answers are dictionary and arrays but this is precisely what you want.

Answer this question