Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does this table not print the names?

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..

2 comments

0

Change the iterator function to pairs. iPairs is for iterating tables with numerical indices whereas your table has string indices.

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.

1 answer

✓ Accepted answer

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

1 comment

0

Thank you Creeper! this was the answer I was looking for. Even though I figured it out the day after I posted.

This discussion is preserved as it was. New answers, comments, and votes are closed.