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

Lua Press lever it transforms item

Tbol

Well-Known Member
Joined
Apr 7, 2019
Messages
625
Reaction score
71
TFS 1.2. Need a lever script when you drop x2 items with specific id to cordinates x,y,z and pressing lever it transforms those two items to one item and spawns it to cordinate x,y,z
 
Solution
Also fixed my one - remember to change Positions and itemids
LUA:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local firstItem = Tile(Position(32393, 32191, 7)):getTopDownItem()
    local secondItem = Tile(Position(32394, 32191, 7)):getTopDownItem()
    if firstItem and secondItem then
        if firstItem.itemid == 2148 and secondItem.itemid == 2152 then
            Game.createItem(2160, 1, Position(32396, 32191, 7))
            firstItem:remove()
            secondItem:remove()
        end
    end
    if item.itemid == 1946 then
        item:transform(1945)
        return false
    end
    item:transform(1946)
    return true
end
Try:

LUA:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local itemCounter = 0
    local stackItems = Tile(Position(32393, 32191, 7)):getItems() -- Tile with items to combine

    if item.itemid == 1946 then
        item:transform(1945)
        return false
    end

    for i = 1, #stackItems do
        local item = stackItems[i]
        local itemId = item:getId()
        if isInArray({2148, 2152}, itemId) then
            itemCounter = itemCounter + 1
            item:remove()
        end
    end
    if itemCounter == 2 then
        Game.createItem(2160, 1, Position(32394, 32191, 7)) -- Tile where item will be created
    end
    item:transform(1946)
    return true
end

XML:
    <action actionid="25500" script="others/testing.lua" />
 
2 items in different positions? or you drop both items to same position?
 
2 items in different positions? or you drop both items to same position?
2 items in different positions. 327, 902, 7 and 328, 902, 7 and then it should spawn in 329, 904, 7
Post automatically merged:

Try:

LUA:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local itemCounter = 0
    local stackItems = Tile(Position(32393, 32191, 7)):getItems() -- Tile with items to combine

    if item.itemid == 1946 then
        item:transform(1945)
        return false
    end

    for i = 1, #stackItems do
        local item = stackItems[i]
        local itemId = item:getId()
        if isInArray({2148, 2152}, itemId) then
            itemCounter = itemCounter + 1
            item:remove()
        end
    end
    if itemCounter == 2 then
        Game.createItem(2160, 1, Position(32394, 32191, 7)) -- Tile where item will be created
    end
    item:transform(1946)
    return true
end

XML:
    <action actionid="25500" script="others/testing.lua" />
Close but it should be two of these \/ i think because they are not stacked they close to each other
local stackItems = Tile(Position(32393, 32191, 7)):getItems() -- Tile with items to combine
 
Try this one
LUA:
local config = {
    [1] = {uid = 3045, position = Position(327, 902, 7), itemId = 2033},
    [2] = {position = Position(328, 902, 7)}
}

local function revertLever(position)
    local leverItem = Tile(position):getItemById(1946)
    if leverItem then
        leverItem:transform(1945)
    end
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item.itemid ~= 1945 then
        return true
    end
    if item.uid == config[1].uid then
        local ItemToUse = Tile(config[1].position):getItemById(2160)
        local ItemToUseTwo = Tile(config[2].position):getItemById(2160)
            if ItemToUse and ItemToUseTwo then
                ItemToUse:remove(1)
                ItemToUseTwo:remove(1)
                config[1].position:sendMagicEffect(CONST_ME_TELEPORT)
                config[2].position:sendMagicEffect(CONST_ME_TELEPORT)
                Game.createItem(2160, 1, Position(329, 903, 7))
                item:transform(1946)
                addEvent(revertLever, 4 * 1000, toPosition)
            elseif not ItemToUse and ItemToUseTwo then
                player:sendCancelMessage('You need to offer both items.')
            else
                player:sendCancelMessage('You have already used this lever!')
            end
            end
end
 
Also fixed my one - remember to change Positions and itemids
LUA:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local firstItem = Tile(Position(32393, 32191, 7)):getTopDownItem()
    local secondItem = Tile(Position(32394, 32191, 7)):getTopDownItem()
    if firstItem and secondItem then
        if firstItem.itemid == 2148 and secondItem.itemid == 2152 then
            Game.createItem(2160, 1, Position(32396, 32191, 7))
            firstItem:remove()
            secondItem:remove()
        end
    end
    if item.itemid == 1946 then
        item:transform(1945)
        return false
    end
    item:transform(1946)
    return true
end
 
Last edited:
Solution
Also fixed my one - remember to change Positions and itemids
LUA:
local config = {
    [1] = {pos = Position(32393, 32191, 7), itemid = 2148},
    [2] = {pos = Position(32394, 32191, 7), itemid = 2152}
}
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local counter = 0
    for i = 1, #config do
        local tile = Tile(config[i].pos):getTopDownItem()
        if tile and tile.itemid == config[i].itemid then
            counter = counter + 1
            tile:remove()
            if counter == 2 then
                Game.createItem(2160, 1, Position(32396, 32191, 7))
            end
        end
    end
    if item.itemid == 1946 then
        item:transform(1945)
        return false
    end
    item:transform(1946)
    return true
end
The code has a flaw, even if the conditions are not met, it still removes individual items.
the error is in the loop!

First you make sure that the two items are in their correct position, and then if you proceed to remove them

example:
1601134879082.png
 
Back
Top