• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua in pairs

lhc00

New Member
Joined
Mar 23, 2010
Messages
23
Reaction score
0
I would like to know if there is some way to do i jump to next value in "in pairs":
Code:
local thing = {
      [1234] = {1, 2, 3, 4},
      [1236] = {1, 2, 3, 6}
      }
      
for i, v in pairs(thing) do
    print(i, v[i-1233])  -- 
end
E.g: in this script, it will print :
"1234, 1
1236, 3"

I would like the script to print only the second line.

Thx everyone.
 
Last edited:
I guess theres no continue; in lua, like other programing languages...
if you want to abort the loop, you can use break

Lua:
local thing = {
      [1234] = {1, 2, 3, 4},
      [1236] = {1, 2, 3, 6}
      }
      
for i, v in pairs(thing) do
    print(i, v[i-1233])
    break
end
 
I will try explain with this example: i have this script:
Lua:
function onUse(cid, item, pos, itemEx, topos)
         x = 0
         k = 1
         local bakados = { --HERE--
               [{6278}] = {2666, 10, 5097, 2, 8846, 3},
               [{2379}] = {2666, 100}        
		 }
	big_pos = {x = topos.x, y=topos.y, z=topos.z, stackpos = 1}
	coisa = {x = topos.x, y=topos.y, z=topos.z, stackpos = 1}
         for i, j in pairs(bakados) do
                posdotrem = {x = topos.x, y = topos.y, z = topos.z, stackpos = 2}
                for w = 2, #j/2+1 do
                    posdascoisas = {x = topos.x, y = topos.y, z = topos.z, stackpos = w}
                    if getThingFromPos(posdascoisas).itemid == j[k] then
                       if getThingFromPos(posdascoisas).type == j[k+1] then
                          x = x+1
                          doPlayerSendTextMessage(cid, 1, ''..x)
                          if x == #j/2 then
                             for z = 2, #j, 2 do                                          
                                 doRemoveItem(getThingFromPos(posdotrem).uid, j[z])
                             end
                             if k == #j -1 then
                                doCreateItem(i[1], 1, topos)
                             end
                          end
                       else
                           --[B]jump to the next ITEM[/B]
                       end
                    else
                        --[B]jump to the next ITEM[/B]
                    end
                    k = k+2
                end
             
         end
end

It's a craft-item script, it works, except when an item needs the same items to be made(like in this case).
 
Last edited:
This might be easier to understand how to use "i" using exp stages in lua.

Lua:
local config = {
	[{1, 8}] = 5
}
 
function onAdvance(cid, skill, oldLevel, newLevel)
	if skill == SKILL__LEVEL then
		for i, v in pairs(config) do
			if(newLevel >= i[2] and oldLevel <= i[1]) then
				doPlayerSetRate(cid, SKILL__LEVEL, v)
				break
			end
		end
	end
 
	return true
end
 
Last edited:
Lua:
	for i, v in pairs(config) do
		for _, cid in ipairs(getOnlinePlayers()) do
Instead of looping every player for each stage, loop each stage for each player (protip: swap), it'll also fix your misplaced break.
 
Back
Top