local Maps = {
["Bog Town"] = "a",
["Cold Marsh"] = "b"
}
print(Maps[math.random(1,#Maps)])
Error:
invalid argument #2 to 'random' (interval is empty)
Please help.
local Maps = {
["Bog Town"] = "a",
["Cold Marsh"] = "b"
}
print(Maps[math.random(1,#Maps)])
Error:
invalid argument #2 to 'random' (interval is empty)
Please help.
The #
operator gets the length of the array part of a table, but you have only the dictionary part. Instead just make it an array.
local Maps = { "Bog Town", "Cold Marsh" }
print(Maps[math.random(#maps)])
With that I canβt include data like βaβ and βbβ seen in the script I originally posted.
You would probably have to do something like looping through the dictionary and setting the index values in an array then getting a random value from that array and getting that value (index) from the array in the dictionary
I recommend having an array of dictionaries
local Maps = {
{ name = "Bog Town", something = "a" },
{ name = "Cold Marsh", something = "b" }
}
print(Maps[math.random(#Maps)])
I am not sure what the single letters are for, but I recommend a better name for the key than something
.
Hey so I know I marked the solution already, but for anyone else who wants to have a dictionary like I did without having to resort to tables that have no key/index, here:
local DataTable = {
["ct1"] = {
Name = "cool town 1",
data = "add whatever u want here, and it can be accessed"
},
["ct2"] = {
Name = "cool town 2",
data = "add whatever u want here, and it can be accessed"
}
}
local Index = {
"ct1",
"ct2",
}
local chosen = Index[math.random(1, #Index)]
print(DataTable[chosen].Name)