• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Solved Random lever reset, check lever before reset. [actions.xml] [loops/tables]

  • Thread starter Thread starter Xikini
  • Start date Start date
X

Xikini

Guest
The Forgotten Server, version 0.3.7_SVN (Crying Damson)

Alright, I'm terrible with loops, but I'm still trying to use them. :p
The entire script I'm trying to create will be, oh so much easier, if loops are involved. :p

The loop/function/action I'm trying to create is for all the lever's to be randomized, when a 'master lever' is used.
Before they are randomized, I also need to check the current state of the levers. Currently attempting to patch in..
Code:
if getTileItemById(pos[1], config.item).uid == 1945 then
   if getTileItemById(pos[2], config.item).uid == 1945 then
     if getTileItemById(pos[3], config.item).uid == 1946 then
       if getTileItemById(pos[4], config.item).uid == 1946 then
         doCleanTile(wall[1],false)
       end
     end
   end
end
But of course if someone knows a better way, please show. :p

For reference, this is the test area I'm using for the script.
Z0wynXE.png

The error I'm getting in the initial script is
Code:
[7:21:40.007] [Error - Action Interface]
[7:21:40.007] data/actions/scripts/aaCarlExampleScripts/random switch.lua:onUse
[7:21:40.007] Description:
[7:21:40.008] ...tions/scripts/aaCarlExampleScripts/random switch.lua:21: 'for' limit must be a number
[7:21:40.008] stack traceback:
[7:21:40.008]  ...tions/scripts/aaCarlExampleScripts/random switch.lua:21: in function <...tions/scripts/aaCarlExampleScripts/random switch.lua:20>
This is the current loop (dare I say the word?!).
It's most likely hideous, but I'm trying. :p
Code:
local leverPos = {
   [1] = {x = 1562, y = 1497, z = 8}, -- top left
   [2] = {x = 1563, y = 1497, z = 8},
   [3] = {x = 1565, y = 1497, z = 8},
   [4] = {x = 1566, y = 1497, z = 8}, -- top right 
   [5] = {x = 1562, y = 1498, z = 8}, -- top middle left
   [6] = {x = 1563, y = 1498, z = 8},
   [7] = {x = 1565, y = 1498, z = 8},
   [8] = {x = 1566, y = 1498, z = 8}, -- top middle right 
   [9] = {x = 1562, y = 1500, z = 8}, -- bottom middle left
   [10] = {x = 1563, y = 1500, z = 8},
   [11] = {x = 1565, y = 1500, z = 8},
   [12] = {x = 1566, y = 1500, z = 8}, -- bottom middle right 
   [13] = {x = 1562, y = 1501, z = 8}, -- bottom left
   [14] = {x = 1563, y = 1501, z = 8},
   [15] = {x = 1565, y = 1501, z = 8},
   [16] = {x = 1566, y = 1501, z = 8} -- bottom right
}

function onUse(cid, item, frompos, item2, topos)
   for i = 1, leverPos do
     local check = getTileItemById(leverPos[i]).uid
       if(math.random(1,2) <= 1) then
         doTransformItem(check.uid, 1946)
       else
         doTransformItem(check.uid, 1945)
       end
   end
return true
end
And for clarity, this is my actions.xml
Code:
<action actionid="45152" event="script" value="aaCarlExampleScripts/random switch.lua"/>
Alright, If you have any questions feel free to ask.
I've been trying for awhile, and will still be trying for awhile, but figured after a few hours it was time to get support. :p

Xikini
 
Last edited by a moderator:
TL/DR Simply put, how can I make the levers flop back and forth?

Alright, so the loop is definitely working, however no matter what I try I cannot get all the levers to swap both ways..
I've changed it many times, but currently I am here.. and the lever flops back and forth, I get no error's when the script is loaded, or when the lever is used.
As far as I can tell the 'check == 1946' does not work at all..
But if I put 'check ~= 1946' then all the levers will flop over, with a 'chance' of course..

I don't understand why it's not working.

Code:
local leverPos = {
   [1] = {x = 1562, y = 1497, z = 8}, -- top left
   [2] = {x = 1563, y = 1497, z = 8},
   [3] = {x = 1565, y = 1497, z = 8},
   [4] = {x = 1566, y = 1497, z = 8}, -- top right   
   [5] = {x = 1562, y = 1498, z = 8}, -- top middle left
   [6] = {x = 1563, y = 1498, z = 8},
   [7] = {x = 1565, y = 1498, z = 8},
   [8] = {x = 1566, y = 1498, z = 8}, -- top middle right   
   [9] = {x = 1562, y = 1500, z = 8}, -- bottom middle left
   [10] = {x = 1563, y = 1500, z = 8},
   [11] = {x = 1565, y = 1500, z = 8},
   [12] = {x = 1566, y = 1500, z = 8}, -- bottom middle right   
   [13] = {x = 1562, y = 1501, z = 8}, -- bottom left
   [14] = {x = 1563, y = 1501, z = 8},
   [15] = {x = 1565, y = 1501, z = 8},
   [16] = {x = 1566, y = 1501, z = 8} -- bottom right
}

function onUse(cid, item, frompos, item2, topos)




   for i = 1, #leverPos do
     local check = getThingFromPos(leverPos[i])
     if check == 1946 then
       if(math.random(1,2) <= 1) then
         doTransformItem(check.uid, 1946)
       end
     elseif check == 1945 then
       if(math.random(1,2) <= 1) then
         doTransformItem(check.uid, 1945)
       end
     end
   end
   doTransformItem(item.uid, item.itemid == 9826 and 9825 or 9826)
return true
end
 
Back
Top