• 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!

Solved Lever Bridge Help

Jfrye

Mapper, trying to learn scripting
Joined
Jan 8, 2009
Messages
366
Solutions
5
Reaction score
86
Location
Mexico Missouri
TFS 1.1 CLIENT VERSION 10.77


I have managed to get this script working for the most part. It will create the bridge, but it will
not remove the bridge.

Pull the lever, bridge pops up. Pull lever again, and nothing happens. The switch moves back and forth, but nothing. No errors in the console or anything, just nothing happens.

Also, IF possible how do I make both levers move at the same time? So I pull one lever and they both move. Both levers have actionID 6000

Code:
local posi3 = {x=2385, y=332, z=8} --
poss = {
[1] = {x=2382, y=332, z=8},
[2] = {x=2383, y=332, z=8},
[3] = {x=2384, y=332, z=8}
}

local lever = {
[1] = {x=2378, y=329, z=8},
[2] = {x=2386, y=331, z=8}
}
local itemids = 5770
function onUse(cid, item, fromPosition, itemEx, toPosition)
    if item.itemid == 1945 then
        doTransformItem(itemids,poss[2])
    if getTileItemById(poss[1],7666).itemid ~= nil then
        doTransformItem(getTileItemById(poss[1],7666).uid,itemids)
    end
    if getTileItemById(poss[2],13548).itemid ~= nil then
        doTransformItem(getTileItemById(poss[2],13548).uid,itemids)
    end
    if getTileItemById(poss[3],7834).itemid > 0 then
        doTransformItem(getTileItemById(poss[3],7834).uid,itemids)
    end
    for i=1,#lever do
    if lever.x == fromPosition.x then
        o = i
    end
    end
    if o == 1 then
        b = 2
    else
        b = 1
    end
        doTransformItem(item.uid,item.itemid+1)
        doTransformItem(getTileItemById(lever,1945).uid,1946)
    elseif item.itemid == 1946 then
    for p = 1,#poss do
        doRelocate(poss[p], posi3)
    end
    for z =1,#poss do
        poss[z].stackpos = 254
    if getTileItemById(poss[z]).itemid > 1000 then
        doRemoveItem(getTileItemById(poss[z]).uid)
    end
        poss[z].stackpos = 1
    if getTileItemById(poss[z]).itemid > 1000 then
        doRemoveItem(getTileItemById(poss[z]).uid)
        end

    end
    for i=1,#lever do
    if lever.x == toPosition.x then
        o = i
    end
    end
    if o == 1 then
        b = 2
    else
        b = 1
    end
        doTransformItem(13548,poss[2])
        doTransformItem(13548,poss[1])
        doTransformItem(13548,poss[3])
        doTransformItem(7666,poss[1])
        doTransformItem(7834,poss[3])
        doTransformItem(item.uid,item.itemid-1)
        doTransformItem(getTileItemById(lever,1946).uid,1945)
    end
    return TRUE
end


This is the original code that I started with. https://otland.net/threads/rookgard-rats-lever.50534/#post507355
 
Last edited:
Glad to see you are trying, its late otherwise I would of rewrote it for you, maybe if I have time when I wake up I'll have a chance.
 
Code:
Tiles = {}

function Tiles:new(pos)
    local mt = setmetatable(pos, {__index = Tiles})
    return mt
end

function Tiles:storeID()
    if not self.storedID then
        local tileitem = Tile(self):getGround()
        self.storedID =    tileitem:getId()
        self.tileitem = tileitem
    end
end

function Tiles:changeID(itemid, count)
    if self.tileitem then
        self.tileitem:transform(itemid, count)
    end
end

function Tiles:restoreID()
    if self.tileitem and self.storedID then
        self.tileitem:transform(self.storedID)
        return self.storedID
    end
end

local postable = {
[1] = Tiles:new({x=129, y = 422, z=7}),
[2] = Tiles:new({x=130, y = 422, z=7}),
[3] = Tiles:new({x=129, y = 423, z=7}),
[4] = Tiles:new({x=130, y = 423, z=7})
}
local levers = {
[1] = {x=128, y=425, z=7},
[2] = {x=128, y=420, z=7},
}

function getLever(pos)
    local lever = (Tile(pos):getItemById(1945) or Tile(pos):getItemById(1946)) or false
    return lever
end

local bridgeId = 5770

function onUse(cid, item, fromPosition, itemEx, toPosition)
    local player = Player(cid)
    local item = Item(item.uid)
    if item:getId() == 1945 then
        for i, tile in ipairs(postable) do
            tile:storeID()
            tile:changeID(bridgeId)
        end
        getLever(levers[1]):transform(1946)
        getLever(levers[2]):transform(1946)
    elseif item:getId() == 1946 then
        for i, tile in ipairs(postable) do
            tile:restoreID()
        end
        getLever(levers[1]):transform(1945)
        getLever(levers[2]):transform(1945)
    end
    return true
end

Try this, change the postitions.
 
Last edited:
@MatheusMkalo the script is error free. The bridge will appear as it should, but if I pull the lever again, nothing happens. The lever will move back and forth, but nothing else happens. No errors or anything, Just nothing. Also, only 1 lever moves, not both of them.
 
@MatheusMkalo the script is error free. The bridge will appear as it should, but if I pull the lever again, nothing happens. The lever will move back and forth, but nothing else happens. No errors or anything, Just nothing. Also, only 1 lever moves, not both of them.

Are you sure that you've changed the code? Did you add the actionids? Because the same function used to move 1 lever is used to move the other.

I've tested it and its perfectly fine.
 
Are you sure that you've changed the code? Did you add the actionids? Because the same function used to move 1 lever is used to move the other.

I've tested it and its perfectly fine.

What actionIDs? I stated in my first post that I was using 6000 for the actionID on BOTH levers. Unless I completely overlooked actionIDs in your code, the levers still have actionID 6000.


Also, since the bridge DOES appear when you pull the lever, it makes me feel like it has nothing to do with the actionIDs.

EDIT- Wow, I copied your code to the top of my old script, for easy access to the locations. I didnt realize that I forgot to remove the old code. I fixed that mistake, and yes your script works great. The only thing is that it does not remove the ground borders. I use borders 7834, and 7666. It spawns the bridge under these borders instead of removing them.
 
What actionIDs? I stated in my first post that I was using 6000 for the actionID on BOTH levers. Unless I completely overlooked actionIDs in your code, the levers still have actionID 6000.


Also, since the bridge DOES appear when you pull the lever, it makes me feel like it has nothing to do with the actionIDs.

Yes, I mean it seems that you are still running your old code, it is not possible for my code to work just for 1 lever, as it uses the positions from the table levers to change their id. If it works for one it is going to work for the other, unless you set wrong positions in the table.

Maybe you didn't remove the tag from actions.xml and you have 2 of them there dunno.

Edit: Okay try this if you check everything and still does not work. Change the bridge id on my code to 103, this will make the floor change to id of "dirt" and you can see if its actually running my code, if after you change the id to 103 and still makes a bridge there, my code is not being runned.
 
@MatheusMkalo I edited my post above.

Code:
Tiles = {}

function Tiles:new(pos)
    local mt = setmetatable(pos, {__index = Tiles})
    return mt
end

function Tiles:storeID(tab)
    if not self.storedID then
        local tile = Tile(self)
        local tileitem = tile:getGround()
        self.storedID =    tileitem:getId()
        self.tileitem = tileitem
        self.storedtab = {}
        if tab then
            for i, id in ipairs(tab) do
                local item = tile:getItemById(id)
                if item and item:getId() > 1 then
                    self.storedtab[item:getId()] = {item, item:getPosition()}
                end
            end
        end
    end
end

function Tiles:changeID(itemid, count)
    if self.tileitem then
        self.tileitem:transform(itemid, count)
        for id, item in pairs(self.storedtab) do
            item[1]:remove()
        end
    end
end

function Tiles:restoreID()
    if self.tileitem and self.storedID then
        self.tileitem:transform(self.storedID)
        for id, item in pairs(self.storedtab) do
            self.storedtab[id] = {Item(doCreateItem(id, 1, item[2])), item[2]}
        end
        return self.storedID
    end
end

local postable = {
[1] = Tiles:new({x=129, y = 422, z=7}),
[2] = Tiles:new({x=130, y = 422, z=7}),
[3] = Tiles:new({x=129, y = 423, z=7}),
[4] = Tiles:new({x=130, y = 423, z=7})
}
local levers = {
[1] = {x=128, y=425, z=7},
[2] = {x=128, y=420, z=7},
}

function getLever(pos)
    local lever = Tile(pos):getItemById(1945) or Tile(pos):getItemById(1946) or false
    return lever
end

local bridgeId = 5770

function onUse(cid, item, fromPosition, itemEx, toPosition)
    local player = Player(cid)
    local item = item
    if item:getId() == 1945 then
        for i, tile in ipairs(postable) do
            tile:storeID({7666, 7834})
            tile:changeID(bridgeId)
        end
        getLever(levers[1]):transform(1946)
        getLever(levers[2]):transform(1946)
    elseif item:getId() == 1946 then
        for i, tile in ipairs(postable) do
            tile:restoreID()
        end
        getLever(levers[1]):transform(1945)
        getLever(levers[2]):transform(1945)
    end
    return true
end

That should do the trick.

The border ids are here "tile:storeID({7666, 7834})" in case you wanna change them.
 
Code:
Tiles = {}

function Tiles:new(pos)
    local mt = setmetatable(pos, {__index = Tiles})
    return mt
end

function Tiles:storeID(tab)
    if not self.storedID then
        local tile = Tile(self)
        local tileitem = tile:getGround()
        self.storedID =    tileitem:getId()
        self.tileitem = tileitem
        self.storedtab = {}
        if tab then
            for i, id in ipairs(tab) do
                local item = tile:getItemById(id)
                if item and item:getId() > 1 then
                    self.storedtab[item:getId()] = {item, item:getPosition()}
                end
            end
        end
    end
end

function Tiles:changeID(itemid, count)
    if self.tileitem then
        self.tileitem:transform(itemid, count)
        for id, item in pairs(self.storedtab) do
            item[1]:remove()
        end
    end
end

function Tiles:restoreID()
    if self.tileitem and self.storedID then
        self.tileitem:transform(self.storedID)
        for id, item in pairs(self.storedtab) do
            self.storedtab[id] = {Item(doCreateItem(id, 1, item[2])), item[2]}
        end
        return self.storedID
    end
end

local postable = {
[1] = Tiles:new({x=129, y = 422, z=7}),
[2] = Tiles:new({x=130, y = 422, z=7}),
[3] = Tiles:new({x=129, y = 423, z=7}),
[4] = Tiles:new({x=130, y = 423, z=7})
}
local levers = {
[1] = {x=128, y=425, z=7},
[2] = {x=128, y=420, z=7},
}

function getLever(pos)
    local lever = Tile(pos):getItemById(1945) or Tile(pos):getItemById(1946) or false
    return lever
end

local bridgeId = 5770

function onUse(cid, item, fromPosition, itemEx, toPosition)
    local player = Player(cid)
    local item = item
    if item:getId() == 1945 then
        for i, tile in ipairs(postable) do
            tile:storeID({7666, 7834})
            tile:changeID(bridgeId)
        end
        getLever(levers[1]):transform(1946)
        getLever(levers[2]):transform(1946)
    elseif item:getId() == 1946 then
        for i, tile in ipairs(postable) do
            tile:restoreID()
        end
        getLever(levers[1]):transform(1945)
        getLever(levers[2]):transform(1945)
    end
    return true
end

That should do the trick.

The border ids are here "tile:storeID({7666, 7834})" in case you wanna change them.


That sir did the trick. Thank you for taking the time to fix this.
 
Back
Top