It keeps printing “nil”, do I have to use a for loop? Thanks!
testTable = {
["TEST1"] = {1,2,3,4,1,2,3,4};
["TEST2"] = {4,3,2,1,4,3,2,1};
}
copyTable = testTable[1]
--copyTable --> nil
It keeps printing “nil”, do I have to use a for loop? Thanks!
testTable = {
["TEST1"] = {1,2,3,4,1,2,3,4};
["TEST2"] = {4,3,2,1,4,3,2,1};
}
copyTable = testTable[1]
--copyTable --> nil
On first impression, I think the problem is that testTable[1]
isn’t assigned (as you only assigned testTable["TEST1"]
and testTable["TEST2"]
). You need to reference them first if you want to get a particular value from "TEST1"
or "TEST2"
.
testTable = {
["TEST1"] = {1,2,3,4,1,2,3,4};
["TEST2"] = {4,3,2,1,4,3,2,1};
}
test2 = testTable["TEST2"]
copyTable = test2[1]
But I would want to get the whole "TEST1"
table. Not just a value inside the table.
Simple:
copyTable = testTable["TEST2"]
Oh, so in this table there really isnt an index value called “1”, but there is 2 indexes called “Test1” and “Test2”, what you should do is;
testTable = {
["TEST1"] = {1,2,3,4,1,2,3,4};
["TEST2"] = {4,3,2,1,4,3,2,1};
}
copyTable = testTable.TEST1 -- or testTable["TEST1"]
print(copyTable) -- Output: "{...}"
it all depends by the indexes!
I hope this was helpful!
Answer this message if you have any further questions or problems!
There is no way of getting that table without knowing the name/index? Like testTable[1]
Edit:
I see what @Gamer539099 said now
Edit 2:
If I would want to get a random table from that dictionary, how would I do it? @Gamer539099
Is there any way of getting a random table from that dictionary?
I am just as curious as you regarding how it is done. I mean, there is a way to get a random index, but it involves creating a proper integer array that assigns an integer to each key, as demonstrated by this StackOverflow post.
Also, If you want to loop through the table (as that is what you asked for), you can reference this page I found. Note use pairs()
not ipairs()
.
Ok, thanks! I’ll just rename the things to: 1,2,3,4 etc.
Thanks for all the help guys
yeah, you wanna do this;
local TableWithElements = {
["Hi"] = "Yooo";
["Yello"] = "Yellow";
}
local IndexNames = { "Hi","Yello"}
local ChoosenIndex = IndexNames[math.random(#IndexNames)]
local TakeElement = TableWithElements[ChoosenIndex]
print(TakeElement)
If the names aren’t important just leave them out and create an array or arrays. Then you can get a random index to get a table.
local testTable = {
{1,2,3,4,1,2,3,4};
{4,3,2,1,4,3,2,1};
}
local index = math.random(#testTable)
local copyTable = testTable[index]