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

Why does this table not print the names?

Asked by 6 years ago
local ItemInformation = {
    Rock = {'Tool'},
    WoodSword = {'Weapon'},
    WaterBottle = {'Food'},
    IronSword = {'Weapon'},
    Spear = {'Weapon'},
    IronAxe = {'Tool'},
    StoneAxe = {'Tool'},
    Bow = {'Weapon'},
    Stone = {'Basic'}
}


for _,i in ipairs(ItemInformation) do
    print(i)
end

No error, no printing. It's really pissing me off..

0
Change the iterator function to pairs. iPairs is for iterating tables with numerical indices whereas your table has string indices. RayCurse 1518 — 6y
1
You're creating a table with one value when you use {}. When you print tables, it prints some sort of ID, not the values. Change {} to () or just remove them to be able to print the value. PreciseLogic 271 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago

fun fact: ipairs() stops when a first nil value is reached. (technically a dictionary has infinite nil values in no particular order). simply use pairs()

local ItemInformation = {
    Rock = {'Tool'},
    WoodSword = {'Weapon'},
    WaterBottle = {'Food'},
    IronSword = {'Weapon'},
    Spear = {'Weapon'},
    IronAxe = {'Tool'},
    StoneAxe = {'Tool'},
    Bow = {'Weapon'},
    Stone = {'Basic'}
}
for k,v in pairs(ItemInformation) do
    print(v[1]) --you used tables as your values so you will have to index them. If you don't need t tables, remove all of the `{}` above and remove the `[1]`
end
0
Thank you Creeper! this was the answer I was looking for. Even though I figured it out the day after I posted. Mr_Scrlpt 52 — 5y
Ad

Answer this question