• 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.3 Help with secret passage script action

amoxicilina

Member
Joined
Jun 25, 2010
Messages
72
Reaction score
11
Hi guys, I'm having a problem with this code, I'm learning.
Problem it does not remove the shelf to release the passage, but when clicking on the lever it changes it to the way it was used but after 1 minute it returns it to its previous itemid, someone could help me I would be very grateful <3.

following faulty code.


Lua:
local Lever_secret = Action()
local config = {
    [30010] = {position = Position(32420, 32352, 9), lever1 = 1345, lever2 = 1346, wellId = 1718, time = 60, revert = true},
    [30011] = {position = Position(32298, 32500, 11), lever1 = 1345, lever2 = 1346, wellId = 1718, time = 60, revert = true},
}

local function revertWall(wallPosition, leverPosition)
    local leverItem = Tile(leverPosition):getItemById(config.lever2)
    if leverItem then
        leverItem:transform(config.lever1)
    end

    Game.createItem(config.wellId, 1, wallPosition)
end

local function removeWall(position)
    local tile = position:getTile()
    if not tile then
        return
    end

    local thing = tile:getItemById(config.wellId)
    if thing then
        thing:remove()
        player:sendCancelMessage('Você tem 1 minuto antes que a passagem se feche.')
        position:sendMagicEffect(CONST_ME_MAGIC_RED)
    end
end

function Lever_secret.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local wall = config[item.uid]
    if item.itemid ~= wall.lever1 then
        player:sendCancelMessage('A alavanca já foi usada.\nCorra talvez tenha tempo pra encontrar a passagem aberta.')
        return true
    end

    local wall = config[item.uid]
    removeWall(wall.position)
    if wall.revert then
        addEvent(revertWall, wall.time * 1000, wall.position, toPosition)
    end
    item:transform(wall.lever2)
    return true
end

Lever_secret:uid(30011, 30010)
Lever_secret:register()
 
Solution
View attachment bandicam 2022-03-09 22-59-00-730.mp4

Lua:
local Lever_secret = Action()
local config = {
    [30010] = { -- lever UniqueId (30010)
        itemToBeRemoved = {
            itemId = 1514,
            position = Position(258, 243, 7)
        },
        leverId = {
            left = 9825,
            right = 9826
        },
        reset = {
            revert = true, -- if true, reverts wall & lever, otherwise lever is a one-time use.
            timer = 5 -- seconds
        }
    },
    [30011] = {
        itemToBeRemoved = {
            itemId = 8538,
            position = Position(261, 243, 7)
        },
        leverId = {
            left = 1945,
            right = 1946
        },
        reset = {
            revert = false, -- if true, reverts wall & lever...
Probably line 17
Lua:
local tile = position:getTile()
try changing to
Lua:
local tile = Tile(position)
 
now it gives me this error when i click on the shelf, sorry for the delay in responding. @Xikini



View attachment 66113

Lua:
local wall = config[item.uid]
    if item.itemid ~= wall.lever1 then
        player:sendCancelMessage('A alavanca já foi usada.\nCorra talvez tenha tempo pra encontrar a passagem aberta.')
        return true
    end
You've created your script to use a lever.
So using the bookshelf and getting that message is what is expected in this scenario.

If that's not what you intended, you'll have to change the script / create a new script for this situation.
 
Lua:
local wall = config[item.uid]
    if item.itemid ~= wall.lever1 then
        player:sendCancelMessage('A alavanca já foi usada.\nCorra talvez tenha tempo pra encontrar a passagem aberta.')
        return true
    end
You've created your script to use a lever.
So using the bookshelf and getting that message is what is expected in this scenario.

If that's not what you intended, you'll have to change the script / create a new script for this situation.


before it was not working because the ids of the levers were wrong. now I get the error of having to use the lever 2x to run the script and the lever does not return to the initial id, follow the video for better understanding.

 
Lua:
item:transform(wall.lever2)
I think it has to do with where you're transforming the lever. Try moving this code to line 38 before the wall stuff. Also I don't see that you're transforming the lever back to the original id anywhere
 
Last edited:
View attachment bandicam 2022-03-09 22-59-00-730.mp4

Lua:
local Lever_secret = Action()
local config = {
    [30010] = { -- lever UniqueId (30010)
        itemToBeRemoved = {
            itemId = 1514,
            position = Position(258, 243, 7)
        },
        leverId = {
            left = 9825,
            right = 9826
        },
        reset = {
            revert = true, -- if true, reverts wall & lever, otherwise lever is a one-time use.
            timer = 5 -- seconds
        }
    },
    [30011] = {
        itemToBeRemoved = {
            itemId = 8538,
            position = Position(261, 243, 7)
        },
        leverId = {
            left = 1945,
            right = 1946
        },
        reset = {
            revert = false, -- if true, reverts wall & lever, otherwise lever is a one-time use.
            timer = 5
        }
    },
}

local function createItemAfterTime(itemId, position)
    local tile = Tile(position)
    if not tile then
        print("Error: Tile not found.")
        return
    end
    Game.createItem(itemId, 1, position)
    position:sendMagicEffect(CONST_ME_MAGIC_RED)
end

local function removeItemFromPosition(itemId, position)
    local tile = Tile(position)
    if not tile then
        print("Error: Tile not found.")
        return false
    end
    
    local item = tile:getItemById(itemId)
    if not item then
        print("Error: Item not found.")
        return false
    end
    
    item:remove()
    position:sendMagicEffect(CONST_ME_MAGIC_RED)
    return true
end

local function transformItemAfterTime(currentItemId, newItemId, position)
    local tile = Tile(position)
    if not tile then
        print("Error: Tile not found.")
        return
    end
    
    local item = tile:getItemById(currentItemId)
    if not item then
        print("Error: Item not found.")
        return false
    end
    
    item:transform(newItemId)
    position:sendMagicEffect(CONST_ME_MAGIC_RED)
end

function Lever_secret.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local index = config[item:getUniqueId()]
    if not index then
        print("Error:  Table or lever configured improperly. Missing UniqueId on item or table index.")
        return true
    end
    
    if item:getId() ~= index.leverId.left then
        player:sendCancelMessage("Passageway has already been revealed!")
        return true
    end

    if not removeItemFromPosition(index.itemToBeRemoved.itemId, index.itemToBeRemoved.position) then
        print("Error: removeItemFromPosition failed.")
        return true
    end
        
    if index.reset.revert then
        addEvent(createItemAfterTime, index.reset.timer * 1000, index.itemToBeRemoved.itemId, index.itemToBeRemoved.position)
        addEvent(transformItemAfterTime, index.reset.timer * 1000, index.leverId.right, index.leverId.left, item:getPosition())
        player:sendCancelMessage("Passageway has been opened! You have " .. index.reset.timer .. " seconds to get inside before it closes!")
    else
        player:sendCancelMessage("Passageway has been opened! Go find it!")
    end
    item:transform(index.leverId.right)
    item:getPosition():sendMagicEffect(CONST_ME_MAGIC_RED)
    return true
end

for v, k in pairs(config) do -- uniqueId automatically added from the table
    Lever_secret:uid(v)
end
Lever_secret:register()
 
Solution
Back
Top