• 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 Create lever problem

Progenosis

Member
Joined
Sep 6, 2011
Messages
131
Reaction score
18
I have a script running on my server, the script makes 3 rocks disappears and also the lever, what I want now is to make the lever appear again once the rocks appear again.

At this moment, when the lever is activated the 3 rocks disappear and also the lever, thats ok for me, but after 3 seconds the rocks appear again but not the lever :/

Please help!


Code:
leverpos = {x=913, y=960, z=8, stackpos=1}
-- wall positions - don't change stackpos.
wall0pos = {x=911, y=959, z=8, stackpos=1}
wall1pos = {x=912, y=959, z=8, stackpos=1}
wall2pos = {x=913, y=959, z=8, stackpos=1}
-------------
function del(pos, id)
    local thing = getTileItemById(pos, id).uid
    if thing > 0 then
        doRemoveItem(thing)
    end
end

function reset_walls(pos, id)
    local thing = getTileItemById(pos, id).uid
    if thing < 1 then
        doCreateItem(id, 1, pos)
    end
end

function reset_lever(pos)
    local lev = getTileItemById(pos, 1946).uid
    if lev > 0 then
        doTransformItem(lev, 1945)
    end
end

local eventLever = 0
local eventDel = {}
local eventReset = {}

local pause = 500 -- 0.5 sec.
local reset_after = 3 * 1000 -- 3 seconds
local wall_id = 1304 -- itemid of the walls
local walls = { -- wall positions
    {x=911, y=959, z=8},
    {x=912, y=959, z=8},
    {x=913, y=959, z=8}
}

function onUse(cid, item, fromPosition, itemEx, toPosition)
    if item.itemid == 1945 then
        doRemoveItem(item.uid, 1945)
        doCreatureSay(cid, "En 3 segundos las rocas volveran a su posicion original!", TALKTYPE_ORANGE_2, getCreaturePosition(cid));
        for i  = 1, #walls do
            eventDel[i] = addEvent(del, pause * i, walls[i], wall_id)
            eventReset[i] = addEvent(reset_walls, reset_after + pause * i, walls[i], wall_id)
        end
        eventLever = addEvent(reset_lever, reset_after + pause * #walls, fromPosition)
    elseif item.itemid == 1946 then
        stopEvent(eventLever)
        for i = 1, #eventDel do
            stopEvent(eventDel[i])
        end
        for i = 1, #eventReset do
            stopEvent(eventReset[i])
        end
        for i = 1, #walls do
            reset_walls(walls[i], id)
        end
        doCreateItem(item.uid,1945,leverpos)
    end
    return true
end
 
you cant transform item if you dont have item there.
DoCreate the lever instead of transforming.
 
Last edited:
Idk why you'd want the lever to be removed.. shouldn't it just flip over, like a lever?
_\_ ... _/_
Code:
doRemoveItem(item.uid, 1945)
Code:
doTransformItem(item.uid, 1945)
you cant transform item if you dont have item there.
DoCreate the level instead of transforming.
More then likely the item would have an actionID attached to it as well. If you don't add the actionID back after recreating the lever it wouldn't work as intended.
 
Last edited:
The second parameter in doRemoveItem is for count (how many it should remove).
Code:
doRemoveItem(uid, count)
In the reset lever part it never add's it back though.. so shouldn't it be transforming instead?
 
Would make more sense indeed, but I ment that if you have 1945 as count it will try to remove that amount of items :p
 
I want the lever to be removed so it can't be re-activated while script is running leading to the script to pause, as it just happened.
When a player clicks the lever the rocks start to remove but if the player clicks again the lever before the script ends, then the script just stops and if there were missing rocks it stays that way.
 
In the script.. if the lever id is 1945, then script activates.. If it transforms to 1946.. then transforms back after awhile.. the lever will be unusable, until the lever transforms again.. o_O
 
(easiest way)
Well, Try something along the lines of adding an event when lever:1945 is pulled, which removes the 3 rocks and 1 lever. The event will create the rocks and a lever:1945, with the unique id, after a certain amount of time.

(not really a proper way of doing it, but so you know)
Run a count down timer based on Global Storage, which will count down from a set value all the way to 0. Get it?

So, once lever:1945 is pulled, the timer will start and transforming the lever into 1946. Then, if the timer still hasn't finished and a player pulls the lever:1946, it will send a cancel message.:
elseif item.itemid == 1946 then
if getGlobalStorageValue(...) == 0 then
FUNCTION WHICH CREATES STONES + LEVER
else
doPlayerCancle("Must wait [TIME] before pulling lever again.")
end
end


Hope I helped you some. You can find examples on the forum just by searching. https://otland.net/threads/timerquest-or-quest-with-timer.53821/#post-543334
 
Last edited:
Where did you copy paste this code? Didn't it come with tutorial?
Let me explain this piece of script you are trying to manHandle.

Code:
leverpos = {x=913, y=960, z=8, stackpos=1}  [B]-- location for whatever purpose you use "leverpos"[/B]
wall0pos = {x=911, y=959, z=8, stackpos=1}  [b]-- location for walls i assume[/b]
wall1pos = {x=912, y=959, z=8, stackpos=1}
wall2pos = {x=913, y=959, z=8, stackpos=1}
-------------
function del(pos, id)   [b]-- Custom made function what takes in 2 parameters[/b]
    local thing = getTileItemById(pos, id).uid  [b]-- Gets item >>UID<< using the position and part of item information.[/b]
    [b]-- UID = table version of item, containg all the information about item (example: itemid, actionid, uniqueid, type, count, etc)[/b]
    if thing > 0 then   [b]-- if item uid is bigger than 0 then it means the value is not NIL (meaning item did exist)[/b]
        doRemoveItem(thing) [b]-- if he found item with if statement, this function will remove it.[/b]
    end
end

function reset_walls(pos, id)   [b]-- Custom made function what takes in 2 parameters[/b]
    local thing = getTileItemById(pos, id).uid  [b]-- once again takes item UID, if possible[/b]
    if thing < 1 then   [b]-- This time if thing = nil, (meaning its not true, doesn't exist) something will happen[/b]
        doCreateItem(id, 1, pos)    [b]-- what happens is: it makes item (id = itemid, because this is only id this function accepts), the number 1 means either 2 things(item type (fluidtype or item count)[/b]
    end
end

function reset_lever(pos)   [b]-- Custom made function what takes in 1 parameter[/b]
    local lev = getTileItemById(pos, 1946).uid  [b]-- once again takes item UID, if possible[/b]
    if lev > 0 then [b]-- if item exists, then[/b]
        doTransformItem(lev, 1945)  [b]-- CHANGE that item into different item with different itemid[/b]
    end
end

local eventLever = 0    [b]-- this is variable[/b]
local eventDel = {}     [b]-- this is empty table[/b]
local eventReset = {}   [b]-- this is empty table[/b]

local pause = 500       [b]-- this is variable[/b]
local reset_after = 3 * 1000    [b]-- this is variable[/b]
local wall_id = 1304    [b]-- this is variable[/b]
local walls = {         [b]-- this is table, filled with 3 tables[/b]
    {x=911, y=959, z=8},[b]-- this is table filled with 3 variables[/b]
    {x=912, y=959, z=8},
    {x=913, y=959, z=8}
}

function onUse(cid, item, fromPosition, itemEx, toPosition) [b]-- metaFunction what uses: PlayerMetaTable(uid), itemMetaTable(uid),
-- fromPosition(filled with location where you started the action
-- second item you are using the item on(uid)
-- toPosition(is filled with location where clicked on, when you had the choice to choose the 2nd location)[/b]
    if item.itemid == 1945 then [b]-- When itemid = 1945 (lever) then something happens, else it skips all the lines below until it finds End or Else[/b]
        doRemoveItem(item.uid, 1945)    [b]-- removes the item (it removes lever)[/b]
        doCreatureSay(cid, "En 3 segundos las rocas volveran a su posicion original!", TALKTYPE_ORANGE_2, getCreaturePosition(cid));    [b]-- sais something to player in spansih with orange text[/b]
        for i  = 1, #walls do   [b]-- makes a loop, where variable i=1 and the i changes to +1 the amount of different values you have inside table(walls)[/b]
            eventDel[i] = addEvent(del, pause * i, walls[i], wall_id)   [b]-- makes variable inside the table eventDel and the name of that key is: eventDel[1][/b]
            [b]-- the variable = function addEvent (this function takes in 2parameters: FUNCTION AND the TIME when is should be activated after this line has been read)[/b]
            [b]-- del= function (this is the custom function we have above)[/b]
            [b]-- puase*i = takes the pause varible what is(500) and the variable i what you created with loop, with the first loop, the i is 1 | 500*1 = 500, this means the event will be executed 500milliseconds after you read that line.[/b]
            [b]-- now the addEvent also has 2nd way to execute functions, you can have the variables after timer, so here they come:[/b]
            [b]-- walls[i] aka walls[1] means it will take the first key from table(walls), the key in this case is table({x=911, y=959, z=8}) [/b]
            [b]-- and the 2nd variable is wall_id aka 1304[/b]
            eventReset[i] = addEvent(reset_walls, reset_after + pause * i, walls[i], wall_id)
            [b]-- another addEvent.[/b]
        end
        eventLever = addEvent(reset_lever, reset_after + pause * #walls, fromPosition)  [b]-- Yet another addEVent[/b]
        [b]-- #walls aka the number of how many keys there are in table(walls) which is 3[/b]
    elseif item.itemid == 1946 then [b]-- now this is the statement if the itemid was not 1945, if the above was true and executed, anything above wont be looked at the system will look for END[/b]
        stopEvent(eventLever)   [b]-- function what looks for varible what has event attached to it and the stops it, Im glad you already using it, always do. [/b]
        [b]-- Else if some values suddenly turn to nil in the given event time you will start getting error messages and might break your further codes[/b]
        for i = 1, #eventDel do [b]-- makes a loop and it goes trough all the event variables to stop them[/b]
            stopEvent(eventDel[i])
        end
        for i = 1, #eventReset do   [b]-- same thing[/b]
            stopEvent(eventReset[i])
        end
        for i = 1, #walls do    [b]-- activates custom  function what you created above[/b]
            reset_walls(walls[i], id)
        end
        doCreateItem(item.uid,1945,leverpos)    [b]-- no idea what is this, i guess it takes the item you used to pull lever and makes 1945 copies of that item.[/b]
        [b]-- last line should be like this: doCreateItem(1945, 1, leverpos)[/b]
    end
    return true
end

EDIT: hopefully it helped to understand your script and now you can either make it remove the lever and put it back later, or not allow player to pull when lever is pushed and have to wait until lever is being pushed back automatically, or whatnotelse. There are plenty of options to solve this particular problem.
 
Back
Top