View Full Version : [Tutorial] "for 'something' do" EXPLAINED

03-20-2014, 10:41 PM
Hey All,

Explained how "for i = 1, #Something do" works to ppgab. Thought this may help others as well so decided to post here.

Really apreciate your efforts man ;)

Care to explain what "for i = 1, #RashidLocation do" does? i understood everything else except this

So, heres how that works. RashidLocation is a table of values. Rashid[i] represents these values. If you put Rashid[1] then you will get the first value of the table, Rashid[2] will give you 2nd value of the table. Let me make a smaller table.

Items = { "Wands", "Blue Djiin",
"Rods", "Green Djiin"

For this table, Items[1] is "Wands", Items[2] is "Blue Djiin", Items[3] is "Rods", Items[4] is "Green Djiin".

Now, looking at the following:

ItemToSell = "Rods"

for i = 1, #Items do
if ItemToSell == Items[i] then WhereToSell = Items[i+1] end

"for i = 1, #Items do" <--- for i equaling numbers 1 to 4 (4 being total number of Items table values), "do" whatever follows.

This means lets put i in to the equation each time and see if its true.

if ItemToSell == Items[i] then WhereToSell = Items[i+1] end <-----Original
if ItemToSell == Items[1] then WhereToSell = Items[1+1] end <-- With i = 1, "if ItemToSell == "Wands" then WhereToSell = "BlueDjiin" (Items[2] value)
if ItemToSell == Items[2] then WhereToSell = Items[2+1] end
if ItemToSell == Items[3] then WhereToSell = Items[3+1] end
if ItemToSell == Items[4] then WhereToSell = Items[4+1] end

ItemToSell is "Rods" and will only be true when i = 3, as Items[3] value is "Rods". Therefore we will end up with WhereToSell = Items[3+1] or WhereToSell = "Green Djiin"

Hope this makes sense and helps some people.


03-20-2014, 10:58 PM
the man!

06-11-2014, 05:53 AM
and this: for _, cid in ipairs(tbl) do ?