Was watching a tutorial and the person wrote this code
local playerItems = {
Josh = {"Crossbow", "Axe"}
Jeff = {"Sword", "Shield"}
}
for name, items in playerItems do
for index, value in items do
print (name.." - "..value)
end
end
I looked at it a bunch of times and have no idea what these variables do and what corresponds to what
So basically “for name, items in playerItems” goes through the playerItems table, which has “Jeff” and “Josh” in them. Both have their own table with items in them as you can see. The loop then takes their tables, which you’ve called “items”.
The next for loop, “for index, value in items”, does pretty much the same thing as the one above. It takes the content of the tables Jeff and Josh have.
The print function then basically says which of these two have which items in their own tables.
I hope this kind of cleared it up, I did my best to explain it as good as possible!
Josh = {"Crossbow", "Axe"} -- Josh is a variable inside the playerItems table; Crossbow and Axe are two items inside Josh's table
What the table looks like: playerItems
Josh
a. Crossbow
b. Axe
Jeff
a. Sword
b. Shield
for anyVariableForIndex(each), anyVariableForIndex’sItem(stuff) in playerItems’sTable do-- returns 1 & 2 which are Josh & Jeff for anyVariableForIndexAgain(each), anyVariableforIndex’sItemAgain(thing) in stuff do-- returns a & b which are the weapons names
print(Josh & Jeff - theirThings) end-- close the a&b loop through end-- close the 1&2 loop through
Inside the table, don’t forget to use a comma if there will be more than one item or it will throw an error.
local playerItems = {
Josh = {"Crossbow", "Axe"}, -- comma here
Jeff = {"Sword", "Shield"}
}