• 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 Switch script

Hypomania

New Member
Joined
Jan 9, 2016
Messages
20
Reaction score
1
Hi, I have a simple switch which creates/removes the walls. I want to add a delay for both, removing and creating the walls, but it only works if there is a delay for createWall2 function. if I add a removeWall2 function and use addEvent, it never removes the walls and multiple walls are created on top of the previous ones:

Code:
function onUse(cid, item, fromPosition, itemEx, toPosition) 
    wall_pos1 = {x=1496, y=1488, z=5, stackpos=1}
    wall_pos2 = {x=1497, y=1488, z=5, stackpos=1}   
    wall1 = getThingfromPos(wall_pos1)
    wall2 = getThingfromPos(wall_pos2) 
  
        if item.itemid == 1945 then 
          
            addEvent(createWall2, 1 * 1000)
            doTransformItem(item.uid, 1946)
          
        elseif item.itemid == 1946 and wall1.itemid == 1052 and wall2.itemid == 1052 then
      
          
            addEvent(removeWall2, 1 * 1000)
            doTransformItem(item.uid, 1945)
          
        else
            doPlayerSendCancel(cid,"Sorry, not possible.")
        end 
    return TRUE 
end 
  
function createWall2()   
    doCreateItem(1052, wall_pos1) 
    doSendMagicEffect(wall_pos1, 10)
    doCreateItem(1052, wall_pos2) 
    doSendMagicEffect(wall_pos2, 10)
end

function removeWall2()   
  
      doSendMagicEffect(wall_pos1, 10) 
      doRemoveItem(wall1.uid, 1)
      doSendMagicEffect(wall_pos2, 10) 
      doRemoveItem(wall2.uid, 1)
end

function removeWall2()
 
Not 100% sure on what you want. But maybe something like this:

Code:
function onUse(cid, item, frompos, item2, topos)
    local CFG={
        Thing=1333,
        ThingPos={x=100,y=100,z=7},
        Delay=10*1000,
    }
   
    local Remove=function()
        doRemoveItem(getTileItemById(CFG.ThingPos,CFG.Thing).uid)
    end
   
    local Create=function()
        doRelocate(CFG.ThingPos,{x=CFG.ThingPos.x,y=CFG.ThingPos.y+1,z=CFG.ThingPos.z})
        doCreateItem(CFG.Thing,1,CFG.ThingPos)
    end
   
    local t=item.itemid==1945
    addEvent(t and Remove or Create, CFG.Delay)
    return true, doTransformItem(item.uid, t and 1946 or 1945)
end
 
Code:
local pos = {
    [1] = {x = 1496, y = 1488, z = 5, stackpos = 1},
    [2] = {x = 1497, y = 1488, z = 5, stackpos = 1}
}

local walls, wallId, effect = {}, 1052, 10

function onUse(cid, item, fromPosition, itemEx, toPosition)

    local function createOrRemoveWalls(create)
        for _, p in ipairs(pos) do
            doSendMagicEffect(p, effect)
            if create then
                doCreateItem(wallId, p)
            else
                doRemoveItem(getThingfromPos(p).uid, 1)
            end
        end
    end

    for _, thing in ipairs(pos) do
        walls[#walls + 1] = getThingfromPos(thing)
    end
 
    if item.itemid == 1945 then
        addEvent(createOrRemoveWalls, 1, true)
        doTransformItem(item.uid, 1946)
    elseif item.itemid == 1946 and walls[1].itemid == wallId and walls[2].itemid == wallId then
        addEvent(createOrRemoveWalls, 1, false)
        doTransformItem(item.uid, 1945)
    else
        doPlayerSendCancel(cid,"Sorry, not possible.")
    end
    return true
end
 
Hey guys, sorry for the late response. Thank you for your suggestions, and I have tried both of the codes but none of them work properly. Maybe I should've explained it better:

I want to add a 1 second delay before walls are created OR removed.

Now if I do this it works:
Code:
addEvent(createWall2, 1 * 1000)

But if I do this as well the else doPlayerSendCancel(cid,"Sorry, not possible.") function is never entered, and the walls are created on top of each other :
Code:
addEvent(removeWall2, 1 * 1000)
 
Code:
function onUse(cid, item, frompos, item2, topos)
    local C={
        walls={
            {x=1496,y=1488,z=5},
            {x=1497,y=1488,z=5},
        },
        delay=1*1000
    }
    local func, t = function(v)
        for i=1,2 do
            if v then
                doCreateItem(1052,1,C.walls[i])
            else
                doRemoveItem(getTileItemById(C.walls[i],1052).uid)
            end
            doSendMagicEffect(C.walls[i],10)
        end
    end, item.itemid==1945

    return true, addEvent(func, C.delay, t and true or false), doTransformItem(item.uid, t and 1946 or 1945)
end
 
Last edited:
Back
Top