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

TFS 1.X+ TFS 1.3, Remove item on spot with timer

Joriku

Working in the mines, need something?
Joined
Jul 16, 2016
Messages
1,076
Solutions
15
Reaction score
370
Location
Sweden
YouTube
Joriku
Hi,
This can be done through revscript aswell.
But I've tried alot of diffrent combinations but I can't manage to solve this without getting an null error.
here's the code for one item spot. Trying to make it remove the same item id on two locations.

Lua:
local wallPosition = Position(3097, 2945, 7) --wall position
local wall = 1546 --wall ID
local revertTime = 1 * 60 * 100 --time: 1=1min

local function revertWall()
    Game.createItem(wall, 1, wallPosition)
    wallPosition:sendMagicEffect(CONST_ME_POFF)
    Game.broadcastMessage("Bridge Event Closed.", MESSAGE_EVENT_ADVANCE)
end

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
        local removeWall = Tile(wallPosition):getItemById(wall)
        if removeWall then
            removeWall:remove()
            wallPosition:sendMagicEffect(CONST_ME_POFF)
            Game.broadcastMessage("Bridge Event Open for 30 Minutes!", MESSAGE_EVENT_ADVANCE)
            addEvent(revertWall, revertTime)
        end

        item:transform(1946)
        addEvent(revertLever, revertTime, toPosition)
    end
    return true
end
 
Try.
Note: set correct wallPosition before testing.

What error did you get? which line.

Lua:
local wallPosition = Position(141, 382, 7) --wall position
local wall = 1546 --wall ID
local revertTime = 1 * 60 * 100 --time: 1=1min

local function revertWall()
    Game.createItem(wall, 1, wallPosition)
    wallPosition:sendMagicEffect(CONST_ME_POFF)
    Game.broadcastMessage("Bridge Event Closed.", MESSAGE_EVENT_ADVANCE)
end

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

local action = Action()
function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if not item.itemid == 1945 then
        return true
    end
    local removeWall = Tile(wallPosition):getItemById(wall)
    if removeWall then
        removeWall:remove()
        wallPosition:sendMagicEffect(CONST_ME_POFF)
        Game.broadcastMessage("Bridge Event Open for 30 Minutes!", MESSAGE_EVENT_ADVANCE)
        addEvent(revertWall, revertTime)
    end
    item:transform(1946)
    addEvent(revertLever, revertTime, fromPosition)
    return true
end

action:aid(11111)
action:register()
 
Last edited:
Same as the others above me, I don't see an issue with the code.
Definitely need to see the error.
Try.
Note: set correct wallPosition before testing.

What error did you get? which line.

Lua:
local wallPosition = Position(141, 382, 7) --wall position
local wall = 1546 --wall ID
local revertTime = 1 * 60 * 100 --time: 1=1min

local function revertWall()
    Game.createItem(wall, 1, wallPosition)
    wallPosition:sendMagicEffect(CONST_ME_POFF)
    Game.broadcastMessage("Bridge Event Closed.", MESSAGE_EVENT_ADVANCE)
end

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

local action = Action()
function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if not item.itemid == 1945 then
        return true
    end
    local removeWall = Tile(wallPosition):getItemById(wall)
    if removeWall then
        removeWall:remove()
        wallPosition:sendMagicEffect(CONST_ME_POFF)
        Game.broadcastMessage("Bridge Event Open for 30 Minutes!", MESSAGE_EVENT_ADVANCE)
        addEvent(revertWall, revertTime)
    end
    item:transform(1946)
    addEvent(revertLever, revertTime, fromPosition)
    return true
end

action:aid(11111)
action:register()
post print of the error too
So basically I am trying to make it into two positions Instead of one, I managed to make it remove at both but it continued to add items on the spots and stacked them instead.
In this code it's no error and it works. For ONE position, This Is after I tried.. here's two codes I tried, I did more into them but they're not saved, Any solution for my issue?

First one
Lua:
local wallPosition = Position(3097, 2945, 7) --wall position
local wallPosition2 = Position(3097, 2946, 7) --wall position
local wall = 1546 --wall ID
local revertTime = 1 * 60 * 100 --time: 1=1min

local function revertWall()
    Game.createItem(wall, 1, wallPosition)
    Game.createItem(wall, 1, wallPosition2)
    wallPosition:sendMagicEffect(CONST_ME_POFF)
    wallPosition2:sendMagicEffect(CONST_ME_POFF)
    Game.broadcastMessage("Bridge Event Closed.", MESSAGE_EVENT_ADVANCE)
end

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
        local removeWall = Tile(wallPosition):getItemById(wall) -- No clue how to edit this into wallPosition2 aswell
        if removeWall then
            removeWall:remove()
            wallPosition:sendMagicEffect(CONST_ME_POFF)
            wallPosition2:sendMagicEffect(CONST_ME_POFF)
            Game.broadcastMessage("Bridge Event Open for 30 Minutes!", MESSAGE_EVENT_ADVANCE)
            addEvent(revertWall, revertTime)
        end

        item:transform(1946)
        addEvent(revertLever, revertTime, toPosition)
    end
    return true
end

Second Try:
Lua:
local wallPositions = {
    {x = 3097, y = 2945, x= 7},
    {x = 3097, y = 2946, x= 7}
}

local wall = 1546 --wall ID
local revertTime = 1 * 60 * 100 --time: 1=1min

local function revertWall()
    Game.createItem(wall, 1, wallPositions)
    wallPositions:sendMagicEffect(CONST_ME_POFF)
    Game.broadcastMessage("Bridge Event Closed.", MESSAGE_EVENT_ADVANCE)
end

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
        local removeWall = Tile(wallPositions):getItemById(wall)
        if removeWall then
            removeWall:remove()
            wallPositions:sendMagicEffect(CONST_ME_POFF)
            Game.broadcastMessage("Bridge Event Open for 30 Minutes!", MESSAGE_EVENT_ADVANCE)
            addEvent(revertWall, revertTime)
        end

        item:transform(1946)
        addEvent(revertLever, revertTime, toPosition)
    end
    return true
end
 
Try this. Seems to be working over here.
Lua:
local walls = {
    Position(3097, 2945, 7, STACKPOS_FIRST_ITEM_ABOVE_GROUNDTILE),
    Position(3097, 2946, 7, STACKPOS_FIRST_ITEM_ABOVE_GROUNDTILE)
}

local wallId = 1546 --wall ID
local revertTime = 1 * 10 * 1000 --time: 1=1min

local function revertWall()
    for _, wallPosition in pairs(walls) do
        Game.createItem(wallId, 1, wallPosition)
        wallPosition:sendMagicEffect(CONST_ME_POFF)
    end
    Game.broadcastMessage("Bridge Event Closed.", MESSAGE_EVENT_ADVANCE)
end

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

local action = Action()
function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item.itemid == 1945 then
        local wallsRemoved = false
        for _, wallPosition in pairs(walls) do
            local tile = Tile(wallPosition)
            local wall = tile:getItemById(wallId)
            if tile and wall then
                wall:remove()
                wallPosition:sendMagicEffect(CONST_ME_POFF)
                Game.broadcastMessage("Bridge Event Open for 30 Minutes!", MESSAGE_EVENT_ADVANCE)
                wallsRemoved = true
            end
        end

        if wallsRemoved then
            addEvent(revertWall, revertTime)
        end

        item:transform(1946)
        addEvent(revertLever, revertTime, toPosition)
    end

    return true
end

action:aid(11111)
action:register()
 
Try this. Seems to be working over here.
Lua:
local walls = {
    Position(3097, 2945, 7, STACKPOS_FIRST_ITEM_ABOVE_GROUNDTILE),
    Position(3097, 2946, 7, STACKPOS_FIRST_ITEM_ABOVE_GROUNDTILE)
}

local wallId = 1546 --wall ID
local revertTime = 1 * 10 * 1000 --time: 1=1min

local function revertWall()
    for _, wallPosition in pairs(walls) do
        Game.createItem(wallId, 1, wallPosition)
        wallPosition:sendMagicEffect(CONST_ME_POFF)
    end
    Game.broadcastMessage("Bridge Event Closed.", MESSAGE_EVENT_ADVANCE)
end

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

local action = Action()
function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item.itemid == 1945 then
        local wallsRemoved = false
        for _, wallPosition in pairs(walls) do
            local tile = Tile(wallPosition)
            local wall = tile:getItemById(wallId)
            if tile and wall then
                wall:remove()
                wallPosition:sendMagicEffect(CONST_ME_POFF)
                Game.broadcastMessage("Bridge Event Open for 30 Minutes!", MESSAGE_EVENT_ADVANCE)
                wallsRemoved = true
            end
        end

        if wallsRemoved then
            addEvent(revertWall, revertTime)
        end

        item:transform(1946)
        addEvent(revertLever, revertTime, toPosition)
    end

    return true
end

action:aid(11111)
action:register()
I just want to mention, that since all of these positions and walls are already known in advance, we don't need to do the extra checks to see if they exist.

Lua:
local walls = {
    Position(3097, 2945, 7),
    Position(3097, 2946, 7)
}

local wallId = 1546
local revertTime = 1 * 10 * 1000 -- 10 seconds

local function revertWall()
    for _, wallPosition in pairs(walls) do
        Game.createItem(wallId, 1, wallPosition)
        wallPosition:sendMagicEffect(CONST_ME_POFF)
    end
    Game.broadcastMessage("Bridge Event Closed.", MESSAGE_EVENT_ADVANCE)
end

local function revertLever(position)
    Tile(position):getItemById(1946):transform(1945)
end

local action = Action()
function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item.itemid == 1945 then
        item:transform(1946)
        for _, wallPosition in pairs(walls) do
            Tile(wallPosition):getItemById(wallId):remove()
            wallPosition:sendMagicEffect(CONST_ME_POFF)
        end
        addEvent(revertWall, revertTime)
        addEvent(revertLever, revertTime, toPosition)
        Game.broadcastMessage("Bridge Event Open for 30 Minutes!", MESSAGE_EVENT_ADVANCE)
    end
    return true
end

action:aid(11111)
action:register()
 
I just want to mention, that since all of these positions and walls are already known in advance, we don't need to do the extra checks to see if they exist.

Lua:
local walls = {
    Position(3097, 2945, 7),
    Position(3097, 2946, 7)
}

local wallId = 1546
local revertTime = 1 * 10 * 1000 -- 10 seconds

local function revertWall()
    for _, wallPosition in pairs(walls) do
        Game.createItem(wallId, 1, wallPosition)
        wallPosition:sendMagicEffect(CONST_ME_POFF)
    end
    Game.broadcastMessage("Bridge Event Closed.", MESSAGE_EVENT_ADVANCE)
end

local function revertLever(position)
    Tile(position):getItemById(1946):transform(1945)
end

local action = Action()
function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item.itemid == 1945 then
        item:transform(1946)
        for _, wallPosition in pairs(walls) do
            Tile(wallPosition):getItemById(wallId):remove()
            wallPosition:sendMagicEffect(CONST_ME_POFF)
        end
        addEvent(revertWall, revertTime)
        addEvent(revertLever, revertTime, toPosition)
        Game.broadcastMessage("Bridge Event Open for 30 Minutes!", MESSAGE_EVENT_ADVANCE)
    end
    return true
end

action:aid(11111)
action:register()
I think I am turning into a fanboy, anywhere I can sign up for it?
Love you, thank you. Would you mind giving a lil explanation to where and why you use
Lua:
        for _, wallPosition in pairs(walls) do -- this, for _, ? and in pairs? in pairs means, more than one at the same time?
[/code

and at
[code=lua]
local walls = {
    Position(3097, 2945, 7), -- why those marks ( Instead of the other ones { <--
    Position(3097, 2946, 7)
}
 
I think I am turning into a fanboy, anywhere I can sign up for it?
Love you, thank you. Would you mind giving a lil explanation to where and why you use
Lua:
        for _, wallPosition in pairs(walls) do -- this, for _, ? and in pairs? in pairs means, more than one at the same time?
[/code

and at
[code=lua]
local walls = {
    Position(3097, 2945, 7), -- why those marks ( Instead of the other ones { <--
    Position(3097, 2946, 7)
}
Basically it's the same as saying for v, k in pairs(table) do but since we aren't using v we turn into _ to signify that we don't plan on using it.
pairs, is basically just a fast way to iterate over an array, when you don't care in what order the array is iterated.

In a more literal sense, we are calling this function.
Lua:
function pairs(t)
    return next, t, nil
end

As for the walls array..
You can break up the array into multi-line rows, and it will act the same.

So both of these table examples, are the same table, but look different.
Lua:
local walls = {
    Position(3097, 2945, 7),
    Position(3097, 2946, 7)
}

local walls = {Position(3097, 2945, 7), Position(3097, 2946, 7)}
So we aren't using () in place of {} because we are actually creating a variable Position with data inside of (), instead of creating a new array {}.
 
Back
Top