• 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 Elevator Script Help

Apollos

Dude who does stuff
Joined
Apr 22, 2009
Messages
829
Solutions
123
Reaction score
655
Location
United States
For TFS 1.1:

Having a few issues with an elevator script I've been working on.

First, I can't figure out how to make this part recognize the tiles and only let the player use the elevator if he is standing on those tiles. (Located in first part of script, line 12-15)

Second, I have a text animation through do creature say when you arrive at the floor but it only works half the time, it probably is because I couldn't correctly call multiple items in the tables either. (Located in the second part of the script, lines 24-50)

Lua:
function onUse(cid, words, position, Toposition)
    local player = Player(cid)
    local playerPos = getPlayerPosition(cid)
    local modal = ModalWindow (1901, "Depot Elevator", "Choose a floor:")
    local tilePos =  {
        {x=1232, y=1043, z=7},
        {x=1232, y=1043, z=6},
        {x=1232, y=1043, z=5},
        {x=1232, y=1043, z=4}
    }
  
    if Tile(playerPos) ~= Tile(tilePos) then
        doPlayerSendTextMessage(cid.uid, 18, "You must be inside the elevator.")
        return false
    end
  
    modal:addChoice(1, "Ground Floor")
    modal:addChoice(2, "Floor 1")
    modal:addChoice(3, "Floor 2")
    modal:addChoice(4, "Floor 3")
    if modal:getId() == 1901 then
    modal:addButton(3, "Select")
    modal:addButton(4, "Cancel")
    end
 
    modal:sendToPlayer(player)
    return true
end

Lua:
local pos1 = {x=1232, y=1043, z=7}
local pos2 = {x=1232, y=1043, z=6}
local pos3 = {x=1232, y=1043, z=5}
local pos4 = {x=1232, y=1043, z=4}
local ding1 = {x=1231, y=1043, z=7}
local ding2 = {x=1231, y=1043, z=6}  
local ding3 = {x=1231, y=1043, z=5}  
local ding4 = {x=1231, y=1043, z=4}

local function removeWall1 (p1, itemId)
    local gate1 = getTileItemById(p1, 9486)
    if gate1 then
        doRemoveItem(gate1.uid,1)
        end  
end

local function removeWall2 (p2, itemId)
    local gate2 = getTileItemById(p2, 9486)
    if gate2 then
        doRemoveItem(gate2.uid,1)
        end  
end

local function onDing1(cid)
     local player = Player(cid)
     if player then
         player:say("*ding*", TALKTYPE_MONSTER_SAY, false, nil, Position(ding1))
     end
end

local function onDing2(cid)
     local player = Player(cid)
     if player then
         player:say("*ding*", TALKTYPE_MONSTER_SAY, false, nil, Position(ding2))
     end
end

local function onDing3(cid)
     local player = Player(cid)
     if player then
         player:say("*ding*", TALKTYPE_MONSTER_SAY, false, nil, Position(ding3))
     end
end

local function onDing4(cid)
     local player = Player(cid)
     if player then
         player:say("*ding*", TALKTYPE_MONSTER_SAY, false, nil, Position(ding4))
     end
end

function onModalWindow(cid, modalWindowId, buttonId, choiceId, item, position, pos, toPosition, fromPosition, itemEx)
    local p1 = getPlayerPosition(cid)
        p1.x = p1.x + 1
        p1.y = p1.y + 1
    local p2 = getPlayerPosition(cid)
        p2.y = p2.y + 1  
      
    if modalWindowId == 1901 and buttonId ~= 3 then
    return false
    end
    if modalWindowId == 1901 and choiceId == 1 then
    addEvent(doCreateItem, 1, 9486, 1, p1)
    addEvent(doCreateItem, 500, 9486, 1, p2)
    addEvent(doPlayerSendTextMessage, 1000, cid.uid, 18, "You have arrived at the ground floor.")
    addEvent(doTeleportThing, 1000, cid.uid, pos1, true)
    addEvent(onDing1, 1000, cid.uid)
    addEvent(removeWall2, 1500, p2, 9486)
    addEvent(removeWall1, 2000, p1, 9486)
    return true
    end
    if modalWindowId == 1901 and choiceId == 2 then
    addEvent(doCreateItem, 1, 9486, 1, p1)
    addEvent(doCreateItem, 500, 9486, 1, p2)
    addEvent(doPlayerSendTextMessage, 1000, cid.uid, 18, "You have arrived at Floor 1.")
    addEvent(doTeleportThing, 1000, cid.uid, pos2, true)
    addEvent(onDing2, 1000, cid.uid)
    addEvent(removeWall2, 1500, p2, 9486)
    addEvent(removeWall1, 2000, p1, 9486)
    return true
    end
    if modalWindowId == 1901 and choiceId == 3 then
    addEvent(doCreateItem, 1, 9486, 1, p1)
    addEvent(doCreateItem, 500, 9486, 1, p2)
    addEvent(doPlayerSendTextMessage, 1000, cid.uid, 18, "You have arrived at Floor 2.")
    addEvent(doTeleportThing, 1000, cid.uid, pos3, true)
    addEvent(onDing3, 1000, cid.uid)
    addEvent(removeWall2, 1500, p2, 9486)
    addEvent(removeWall1, 2000, p1, 9486)
    return true
    end
    if modalWindowId == 1901 and choiceId == 4 then
    addEvent(doCreateItem, 1, 9486, 1, p1)
    addEvent(doCreateItem, 500, 9486, 1, p2)
    addEvent(doPlayerSendTextMessage, 1000, cid.uid, 18, "You have arrived at Floor 3.")
    addEvent(doTeleportThing, 1000, cid.uid, pos4, true)
    addEvent(onDing4, 1000, cid.uid)
    addEvent(removeWall2, 1500, p2, 9486)
    addEvent(removeWall1, 2000, p1, 9486)
    return true
    end
end
 
Last edited:
Solution
Are you running Linux?
Lua:
local modalWindowTargetId = 1901
local elevatorConfig = {
    [1] = { -- choiceID matched with modal window
        position = Position(1232, 1043, 7), -- position to teleport per floor
        dingPos = Position(1231, 1043, 7), -- position of switch  on each floor where ding displays on arrival
        msg = "You have arrived at the ground floor.",
    },
    [2] = {
        position = Position(1232, 1043, 6),
        dingPos = Position(1231, 1043, 6),
        msg = "You have arrived at Floor 1.",
    },
    [3] = {
        position = Position(1232, 1043, 5),
        dingPos = Position(1231, 1043, 5),
        msg = "You have arrived at Floor 2.",
    },
    [4] = {
        position = Position(1232, 1043, 4)...
Are you running Linux?
Lua:
local modalWindowTargetId = 1901
local elevatorConfig = {
    [1] = { -- choiceID matched with modal window
        position = Position(1232, 1043, 7), -- position to teleport per floor
        dingPos = Position(1231, 1043, 7), -- position of switch  on each floor where ding displays on arrival
        msg = "You have arrived at the ground floor.",
    },
    [2] = {
        position = Position(1232, 1043, 6),
        dingPos = Position(1231, 1043, 6),
        msg = "You have arrived at Floor 1.",
    },
    [3] = {
        position = Position(1232, 1043, 5),
        dingPos = Position(1231, 1043, 5),
        msg = "You have arrived at Floor 2.",
    },
    [4] = {
        position = Position(1232, 1043, 4),
        dingPos = Position(1231, 1043, 4),
        msg = "You have arrived at Floor 3.",
    }
}

local function onDing(playerID, position, dingPos)
    local player = Player(playerID)
    if player and player:getPosition() == position then
        player:say("*ding*", TALKTYPE_MONSTER_SAY, false, dingPos)
    end
end

local function removeWall(wallPosition, wallId)
    local tile = Tile(wallPosition)
    local item = tile and tile:getItemById(wallId)
    if item then
        item:remove()
    end
end

function onModalWindow(cid, modalWindowId, buttonId, choiceId, item, position, pos, toPosition, fromPosition, itemEx)
    if modalWindowId ~= modalWindowTargetId then
        return
    end

    if buttonId == 2 then
        return
    end

    local elevatorConfig = elevatorConfig[choiceId]
    if not elevatorConfig then
        return print("player was able to choose elevator choice which does not exist: " .. choiceId)
    end

    local player = Player(cid)
    local playerPos = player:getPosition()
    local positionA = Position(playerPos.x + 1, playerPos.y + 1, playerPos.z)
    local positionB = Position(playerPos.x, playerPos.y + 1, playerPos.z)
    local playerId = player:getId()

    Game.createItem(9486, 1, positionA)
    addEvent(function (positionB) Game.createItem(9486, 1, positionB) end, 500, positionB)
    addEvent(function (playerId, msg) local player = Player(cid) return player ~= nil and player:sendTextMessage(MESSAGE_STATUS_WARNING, msg) or false end, 1000, playerId, elevatorConfig.msg)
    addEvent(function (playerId, position) local player = Player(cid) return player ~= nil and player:teleportTo(position) or false end, 1000 playerId, elevatorConfig.position)
    addEvent(onDing, 1010, playerId, elevatorConfig.position, elevatorConfig.dingPos)
    addEvent(removeWall, 2000, positionA, 9486)
    addEvent(removeWall, 1500, positionB, 9486)

    return true
end

Lua:
local modalWindow = {
    id = 1901,
    name = "Depot Elevator",
    title = "Choose a floor:",
    choice = {
        "Ground Floor",
        "Floor 1",
        "Floor 2",
        "Floor 3",
    },
    button = {
        "Select",
        "Cancel",
    }
}

local tilePositions = { -- elevator positions
    Position(1232, 1043, 7),
    Position(1232, 1043, 6),
    Position(1232, 1043, 5),
    Position(1232, 1043, 4)
}

local function isOnElevator(position)
    for i = 1, #tilePositions do
        if position == tilePositions[i] then
            return true
        end
    end
    return false
end

function onUse(cid, item, fromPosition, target, toPosition, isHotkey)
    local player = Player(cid)
    if not isOnElevator(player:getPosition()) then
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "You must be inside the elevator.")
        return false
    end

    local modal = ModalWindow(modalWindow.id, modalWindow.name, modalWindow.title)
    for choiceID, choiceText in pairs(modalWindow.choice) do
        modal:addChoice(choiceID, choiceText)
    end

    for buttonID, buttonText in pairs(modalWindow.button) do
        modal:addButton(buttonID, buttonText)
    end

    return modal:sendToPlayer(player)
end
 
Solution
Are you running Linux?
Lua:
local modalWindowTargetId = 1901
local elevatorConfig = {
    [1] = { -- choiceID matched with modal window
        position = Position(1232, 1043, 7), -- position to teleport per floor
        dingPos = Position(1231, 1043, 7), -- position of switch  on each floor where ding displays on arrival
        msg = "You have arrived at the ground floor.",
    },
    [2] = {
        position = Position(1232, 1043, 6),
        dingPos = Position(1231, 1043, 6),
        msg = "You have arrived at Floor 1.",
    },
    [3] = {
        position = Position(1232, 1043, 5),
        dingPos = Position(1231, 1043, 5),
        msg = "You have arrived at Floor 2.",
    },
    [4] = {
        position = Position(1232, 1043, 4),
        dingPos = Position(1231, 1043, 4),
        msg = "You have arrived at Floor 3.",
    }
}

local function onDing(playerID, position, dingPos)
    local player = Player(playerID)
    if player and player:getPosition() == position then
        player:say("*ding*", TALKTYPE_MONSTER_SAY, false, dingPos)
    end
end

local function removeWall(wallPosition, wallId)
    local tile = Tile(wallPosition)
    local item = tile and tile:getItemById(wallId)
    if item then
        item:remove()
    end
end

function onModalWindow(cid, modalWindowId, buttonId, choiceId, item, position, pos, toPosition, fromPosition, itemEx)
    if modalWindowId ~= modalWindowTargetId then
        return
    end

    if buttonId == 2 then
        return
    end

    local elevatorConfig = elevatorConfig[choiceId]
    if not elevatorConfig then
        return print("player was able to choose elevator choice which does not exist: " .. choiceId)
    end

    local player = Player(cid)
    local playerPos = player:getPosition()
    local positionA = Position(playerPos.x + 1, playerPos.y + 1, playerPos.z)
    local positionB = Position(playerPos.x, playerPos.y + 1, playerPos.z)
    local playerId = player:getId()

    Game.createItem(9486, 1, positionA)
    addEvent(function (positionB) Game.createItem(9486, 1, positionB) end, 500, positionB)
    addEvent(function (playerId, msg) local player = Player(cid) return player ~= nil and player:sendTextMessage(MESSAGE_STATUS_WARNING, msg) or false end, 1000, playerId, elevatorConfig.msg)
    addEvent(function (playerId, position) local player = Player(cid) return player ~= nil and player:teleportTo(position) or false end, 1000 playerId, elevatorConfig.position)
    addEvent(onDing, 1010, playerId, elevatorConfig.position, elevatorConfig.dingPos)
    addEvent(removeWall, 2000, positionA, 9486)
    addEvent(removeWall, 1500, positionB, 9486)

    return true
end

Lua:
local modalWindow = {
    id = 1901,
    name = "Depot Elevator",
    title = "Choose a floor:",
    choice = {
        "Ground Floor",
        "Floor 1",
        "Floor 2",
        "Floor 3",
    },
    button = {
        "Select",
        "Cancel",
    }
}

local tilePositions = { -- elevator positions
    Position(1232, 1043, 7),
    Position(1232, 1043, 6),
    Position(1232, 1043, 5),
    Position(1232, 1043, 4)
}

local function isOnElevator(position)
    for i = 1, #tilePositions do
        if position == tilePositions[i] then
            return true
        end
    end
    return false
end

function onUse(cid, item, fromPosition, target, toPosition, isHotkey)
    local player = Player(cid)
    if not isOnElevator(player:getPosition()) then
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "You must be inside the elevator.")
        return false
    end

    local modal = ModalWindow(modalWindow.id, modalWindow.name, modalWindow.title)
    for choiceID, choiceText in pairs(modalWindow.choice) do
        modal:addChoice(choiceID, choiceText)
    end

    for buttonID, buttonText in pairs(modalWindow.button) do
        modal:addButton(buttonID, buttonText)
    end

    return modal:sendToPlayer(player)
end

No I'm just connecting to localhost through my windows computer, having bought dedicated or anything yet.

Scripts working well, tweaking it now. Wondering if in this instance there a better way to set the direction player direction when he teleports besides making a new function then another addevent?

EDIT: Always seem to have the good ideas after i post the question. lol

Here's completed script as of now, free for anyone to use of course. Thanks again everyone.

Lua:
local elevatorConfig = {
    modalWindow = {
        mwID = 1901,
        name = "Depot Elevator",
        title = "Choose a floor:",
        choice = {
            "Ground Floor",
            "Floor 1",
            "Floor 2",
            "Floor 3",
        },
        button = {
            "Select",
            "Cancel",
        }
    },
}

local tilePositions = {
    Position(1232, 1043, 7),
    Position(1232, 1043, 6),
    Position(1232, 1043, 5),
    Position(1232, 1043, 4)
}

local function isOnElevator(position)
    for i = 1, #tilePositions do
        if position == tilePositions[i] then
            return true
        end
    end
    return false
end

function onUse(cid, item, fromPosition, target, toPosition, isHotkey)
    local modalConfig = elevatorConfig.modalWindow
    local modal = ModalWindow(modalConfig.mwID, modalConfig.name, modalConfig.title)

    local player = Player(cid)
    if not isOnElevator(player:getPosition()) then
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "You must be inside the elevator.")
        return false
    end

    for choiceID, choiceText in pairs(modalConfig.choice) do
        modal:addChoice(choiceID, choiceText)
    end

    for buttonID, buttonText in pairs(modalConfig.button) do
        modal:addButton(buttonID, buttonText)
    end

    return modal:sendToPlayer(player)
end

Lua:
local modalWindowTargetId = 1901
local elevatorConfig = {
    [1] = {
        position = Position(1232, 1043, 7),
        dingPos = Position(1231, 1043, 7),
        msg = "You have arrived at the ground floor.",
    },
    [2] = {
        position = Position(1232, 1043, 6),
        dingPos = Position(1231, 1043, 6),
        msg = "You have arrived at Floor 1.",
    },
    [3] = {
        position = Position(1232, 1043, 5),
        dingPos = Position(1231, 1043, 5),
        msg = "You have arrived at Floor 2.",
    },
    [4] = {
        position = Position(1232, 1043, 4),
        dingPos = Position(1231, 1043, 4),
        msg = "You have arrived at Floor 3.",
    }
}

local function onDing(playerID, position, dingPos)
    local player = Player(playerID)
    if player and player:getPosition() == position then
        player:say("*ding*", TALKTYPE_MONSTER_SAY, nil, false, dingPos)
    end
end

local function removeWall(wallPosition, wallId)
    local tile = Tile(wallPosition)
    local item = tile and tile:getItemById(wallId)
    if item then
        item:remove()
    end
end

function onModalWindow(cid, modalWindowId, buttonId, choiceId, item, position, pos, toPosition, fromPosition, itemEx)
    if modalWindowId ~= modalWindowTargetId then
        return
    end

    if buttonId ~= 1 then
        return
    end
   
    local player = Player(cid)
    local playerPos = player:getPosition()
    local elevatorConfig = elevatorConfig[choiceId]
    if playerPos == elevatorConfig.position then
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "You are already on this floor.")
        return false
    end

    if not elevatorConfig then
        return print("player was able to choose elevator choice which does not exist: " .. choiceId)
    end

    local positionA = Position(playerPos.x + 1, playerPos.y + 1, playerPos.z)
    local positionB = Position(playerPos.x, playerPos.y + 1, playerPos.z)
    local playerId = player:getId()

    Game.createItem(9486, 1, positionA)
    addEvent(function (positionB) Game.createItem(9486, 1, positionB) end, 500, positionB)
    addEvent(function (playerId, msg) local player = Player(cid) return player ~= nil and player:sendTextMessage(MESSAGE_STATUS_WARNING, msg) or false end, 1000, playerId, elevatorConfig.msg)
    addEvent(function (playerId, position) local player = Player(cid) return player ~= nil and player:teleportTo(position) and player:setDirection(SOUTH) or false end, 1000, playerId, elevatorConfig.position)
    addEvent(onDing, 1001, playerId, elevatorConfig.position, elevatorConfig.dingPos)
    addEvent(removeWall, 2000, positionA, 9486)
    addEvent(removeWall, 1500, positionB, 9486)
    return true
end
 
Last edited:
No I'm just connecting to localhost through my windows computer, having bought dedicated or anything yet.

Scripts working well, tweaking it now. Wondering if in this instance there a better way to set the direction player direction when he teleports besides making a new function then another addevent?

EDIT: Always seem to have the good ideas after i post the question. lol

Here's completed script as of now, free for anyone to use of course. Thanks again everyone.

Lua:
local elevatorConfig = {
    modalWindow = {
        mwID = 1901,
        name = "Depot Elevator",
        title = "Choose a floor:",
        choice = {
            "Ground Floor",
            "Floor 1",
            "Floor 2",
            "Floor 3",
        },
        button = {
            "Select",
            "Cancel",
        }
    },
}
local tilePositions = { -- elevator positions
    Position(1232, 1043, 7),
    Position(1232, 1043, 6),
    Position(1232, 1043, 5),
    Position(1232, 1043, 4)
}
local function isOnElevator(position)
    for i = 1, #tilePositions do
        if position == tilePositions[i] then
            return true
        end
    end
    return false
end
function onUse(cid, item, fromPosition, target, toPosition, isHotkey)
    local modalConfig = elevatorConfig.modalWindow
    local modal = ModalWindow(modalConfig.mwID, modalConfig.name, modalConfig.title)
    local player = Player(cid)
    if not isOnElevator(player:getPosition()) then
        player:sendTextMessage(MESSAGE_STATUS_WARNING, "You must be inside the elevator.")
        return false
    end
    for choiceID, choiceText in pairs(modalConfig.choice) do
        modal:addChoice(choiceID, choiceText)
    end
    for buttonID, buttonText in pairs(modalConfig.button) do
        modal:addButton(buttonID, buttonText)
    end
    return modal:sendToPlayer(player)
end

Lua:
local modalWindowTargetId = 1901
local elevatorConfig = {
    [1] = { -- choiceID matched with modal window
        position = Position(1232, 1043, 7), -- position to teleport per floor
        dingPos = Position(1231, 1043, 7), -- position of switch  on each floor where ding displays on arrival
        msg = "You have arrived at the ground floor.",
    },
    [2] = {
        position = Position(1232, 1043, 6),
        dingPos = Position(1231, 1043, 6),
        msg = "You have arrived at Floor 1.",
    },
    [3] = {
        position = Position(1232, 1043, 5),
        dingPos = Position(1231, 1043, 5),
        msg = "You have arrived at Floor 2.",
    },
    [4] = {
        position = Position(1232, 1043, 4),
        dingPos = Position(1231, 1043, 4),
        msg = "You have arrived at Floor 3.",
    }
}
local function onDing(playerID, position, dingPos)
    local player = Player(playerID)
    if player and player:getPosition() == position then
        player:say("*ding*", TALKTYPE_MONSTER_SAY, false, dingPos)
    end
end
local function removeWall(wallPosition, wallId)
    local tile = Tile(wallPosition)
    local item = tile and tile:getItemById(wallId)
    if item then
        item:remove()
    end
end
function onModalWindow(cid, modalWindowId, buttonId, choiceId, item, position, pos, toPosition, fromPosition, itemEx)
    if modalWindowId ~= modalWindowTargetId then
        return
    end
    if not buttonId == 1 then
        return
    end
    local elevatorConfig = elevatorConfig[choiceId]
    if not elevatorConfig then
        return print("player was able to choose elevator choice which does not exist: " .. choiceId)
    end
    local player = Player(cid)
    local playerPos = player:getPosition()
    local positionA = Position(playerPos.x + 1, playerPos.y + 1, playerPos.z)
    local positionB = Position(playerPos.x, playerPos.y + 1, playerPos.z)
    local playerId = player:getId()
    Game.createItem(9486, 1, positionA)
    addEvent(function (positionB) Game.createItem(9486, 1, positionB) end, 500, positionB)
    addEvent(function (playerId, msg) local player = Player(cid) return player ~= nil and player:sendTextMessage(MESSAGE_STATUS_WARNING, msg) or false end, 1000, playerId, elevatorConfig.msg)
    addEvent(function (playerId, position) local player = Player(cid) return player ~= nil and player:teleportTo(position) and player:setDirection(SOUTH) or false end, 1000, playerId, elevatorConfig.position)
    addEvent(onDing, 1001, playerId, elevatorConfig.position, elevatorConfig.dingPos)
    addEvent(removeWall, 2000, positionA, 9486)
    addEvent(removeWall, 1500, positionB, 9486)
    return true
end

A tip don't remove the new lines around functions and if statments, it makes the code look messy and can be harder to read in certain situations.
 
Back
Top