• 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 A loop

jestem pro

That is the question
Joined
Apr 20, 2013
Messages
650
Solutions
14
Reaction score
88
Hi everbody. I'm making a script and I have a problem with the loop. Ok so the script is to movevent function onAddItem. I don't know how i can make that a item will create on the position of the few, because it creates only the first position of the table.

Here the piece:

LUA:
local pos = {
{x=1000, y=1000, z=7},
{x=1001, y=1001, z=7}
}

for _, k in ipairs(pos) do

   if moveitem.itemid == 2669 then
   doCreateItem(8046 ,1, k)
   end
end

so as I lay item on a one of these positions( from table), it creates another item on the position.

Thanks and help me

Sorry my english.
 
itsn't something like this better?
Code:
function onAddItem(moveitem, tileitem, position)
   local pos = {
     {x=1000, y=1000, z=7},
     {x=1001, y=1001, z=7}
   }

   if moveitem.itemid == 2669 then
     doCreateItem(8046 ,1, pos[math.random(2)])
   end
   
end
 
itsn't something like this better?
Code:
function onAddItem(moveitem, tileitem, position)
   local pos = {
     {x=1000, y=1000, z=7},
     {x=1001, y=1001, z=7}
   }

   if moveitem.itemid == 2669 then
     doCreateItem(8046 ,1, pos[math.random(2)])
   end

end
I think that is a different thing that he want ... he want create a item in all positions in the table, and not a random pos....


You should try use something like it:
Code:
local pos = {
    {x=1000, y=1000, z=7},
    {x=1001, y=1001, z=7}
}

for i = 1, #pos  do
    if moveitem.itemid == 2669 then
        doCreateItem(8046 ,1)
    end
end
 
I've updated my comment two times, sorry for it, I'm busy, but I want help you, try use my code ;)
if won't work tell me
 
Nothing happened bro. You can help me it is the most important :D

Now i have so that it creates on the all positions.
Hm sorry it must be inversely. It creates on the one position. hmmm When I lay the item on the first position (from the table up) it creates a item only this position, not all and when I lay a item on the second position(from the table up) it creates a item only this second position.

so it hasn't to check the position item?

Hm I think you understand.
 
Code:
local posT = {        -- positions where itemID is created
   {x=1000, y=1000, z=7},
   {x=1001, y=1001, z=7},
}
local itemID = 8046       -- item:getId() of item what will be created
local count = 1           -- amount of item that will be created (item must be stackable, else its itemType)
local requiredID = 2669     -- item:getId() of item what is should be moved

function onAddItem(moveitem, tileitem, position)
  
    if moveitem:getId() == requiredID then
          for _, pos in pairs(posT) do
                Game.createItem(itemID, count, pos)
          end
    end
end
 
White evo always wants to go and make it complicated just like Codeex

Code:
local pos = {
{x=1000, y=1000, z=7},
{x=1001, y=1001, z=7}
}

for i = 1, #pos do

if moveitem.itemid == 2669 then
doCreateItem(8046 ,1, pos[k])
end
end
 
complicated?
my code is self explanatory in most part.
your code makes not so good scripters wonder what your script is suppose to do.

your small codes make us ask questions.
+ you are checking the itemID twice with no reason.
+ your code will not work because k is nil value
+ takes a little more time to change your code.

Yes perhaps my code is overkill in most part, but its clean and good practice for bigger scripts if he ever decides to script more.
 
@whitevo it's 8.6

yea guys I forgot it's 8.6

@Itutorial whence does it gets pos[k] "k"??

Ok this is all:
Code:
function onAddItem(moveitem, tileitem, position, cid)
local pos = {
{x=1000, y=1000, z=7},
{x=1001, y=1001, z=7}
}

for _, k in ipairs(pos) do

   if moveitem.itemid == 2669 then
   doCreateItem(8046 ,1, k)
    end
  end
return true
end

Now it creates a item on the all positions. And I want that it creates a item only this position on which I lay the item.

Maybe now it is bright. :DD But thanks for efforts.
 
Back
Top