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

Lua Problem with doors and keys

ligus

New Member
Joined
Apr 26, 2010
Messages
253
Reaction score
0
Hey, I can't close/open door by key

key.jpg


Anyway this door is all time opened and everyone can open independently he uses key or not :/

my doors.lua
Code:
local function doorEnter(cid, uid, id, position)
    doTransformItem(uid, id)
    doTeleportThing(cid, position)
    return true
end

function onUse(cid, item, fromPosition, itemEx, toPosition)
    if(fromPosition.x ~= CONTAINER_POSITION and isPlayerPzLocked(cid) and getTileInfo(fromPosition).protection) then
        doPlayerSendDefaultCancel(cid, RETURNVALUE_NOTPOSSIBLE)
        return true
    end

    local locked = DOORS[item.itemid]
    if(locked) then
        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "It is locked.")
        return true
    end

    local door = getItemInfo(item.itemid)
    if(door.levelDoor > 0) then
        if(item.aid == 189) then
            if(not isPremium(cid)) then
                doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Only the worthy may pass.")
                return true
            end

            return doorEnter(cid, item.uid, door.transformUseTo, toPosition)
        end

        local gender = item.aid - 186
        if(isInArray({PLAYERSEX_FEMALE,  PLAYERSEX_MALE}, gender)) then
            if(gender ~= getPlayerSex(cid)) then
                doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Only the worthy may pass.")
                return true
            end

            return doorEnter(cid, item.uid, door.transformUseTo, toPosition)
        end

        local skull = item.aid - 180
        if(skull >= SKULL_NONE and skull <= SKULL_BLACK) then
            if(skull ~= getCreatureSkullType(cid)) then
                doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Only the worthy may pass.")
                return true
            end

            return doorEnter(cid, item.uid, door.transformUseTo, toPosition)
        end

        local group = item.aid - 150
        if(group >= 0 and group < 30) then
            if(group > getPlayerGroupId(cid)) then
                doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Only the worthy may pass.")
                return true
            end

            return doorEnter(cid, item.uid, door.transformUseTo, toPosition)
        end

        local vocation = item.aid - 100
        if(vocation >= 0 and vocation < 50) then
            local vocationEx = getVocationInfo(getPlayerVocation(cid))
            if(vocationEx.id ~= vocation and vocationEx.fromVocation ~= vocation) then
                doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Only the worthy may pass.")
                return true
            end

            return doorEnter(cid, item.uid, door.transformUseTo, toPosition)
        end

        if(item.aid == 190 or (item.aid ~= 0 and getPlayerLevel(cid) >= (item.aid - door.levelDoor))) then
            return doorEnter(cid, item.uid, door.transformUseTo, toPosition)
        end

        doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Only the worthy may pass.")
        return true
    end

    if(door.specialDoor) then
        if(item.aid == 100 or (item.aid ~= 0 and getCreatureStorage(cid, item.aid) > 0)) then
            return doorEnter(cid, item.uid, door.transformUseTo, toPosition)
        end

        doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "The door seems to be sealed against unwanted intruders.")
        return true
    end

    toPosition.stackpos = STACKPOS_TOP_MOVEABLE_ITEM_OR_CREATURE
    local fields, thing = getTileItemsByType(fromPosition, ITEM_TYPE_MAGICFIELD), getThingFromPosition(toPosition)
    if(item.uid ~= thing.uid and thing.itemid >= 100 and table.maxn(fields) ~= 0) then
        return true
    end

    local doorCreature = getThingFromPosition(toPosition)
    if(doorCreature.itemid ~= 0) then
        toPosition.x = toPosition.x + 1
        local query = doTileQueryAdd(doorCreature.uid, toPosition, 20) -- allow to stack outside doors, but not on teleports or floor changing tiles
        if(query == RETURNVALUE_NOTPOSSIBLE) then
            toPosition.x = toPosition.x - 1
            toPosition.y = toPosition.y + 1
            query = doTileQueryAdd(doorCreature.uid, toPosition, 20) -- repeat until found
        end

        if(query ~= RETURNVALUE_NOERROR) then
            doPlayerSendDefaultCancel(cid, query)
            return true
        end

        doTeleportThing(doorCreature.uid, toPosition)
        if(not door.closingDoor) then
            doTransformItem(item.uid, door.transformUseTo)
        end

        return true
    end

    return false
end

Does anyone know whats the problem?
 
Anyone? Or maybe there is a script for opening and closing a door by a special key? Then I will add it one by one...
 
Humm I think, that I should add these lines:
Code:
    if(isInArray(keys, item.itemid)) then
        if(itemEx.actionid > 0) then
            if(item.actionid == itemEx.actionid and doors[itemEx.itemid] ~= nil) then
                doTransformItem(itemEx.uid, doors[itemEx.itemid])
                return true
            end

            doPlayerSendCancel(cid, "The key does not match.")
            return true
        end

        return false
    end
  
    if(doors[item.itemid] ~= nil and checkStackpos(item, fromPosition)) then
        if(item.actionid == 0) then
            doTransformItem(item.uid, doors[item.itemid])
        else
            doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "It is locked.")
        end

        return true
    end



now my doors.lua looks:
Code:
local function doorEnter(cid, uid, id, position)
    doTransformItem(uid, id)
    doTeleportThing(cid, position)
    return true
end

function onUse(cid, item, fromPosition, itemEx, toPosition)
    if(fromPosition.x ~= CONTAINER_POSITION and isPlayerPzLocked(cid) and getTileInfo(fromPosition).protection) then
        doPlayerSendDefaultCancel(cid, RETURNVALUE_NOTPOSSIBLE)
        return true
    end

    local locked = DOORS[item.itemid]
    if(locked) then
        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "It is locked.")
        return true
    end

    local door = getItemInfo(item.itemid)
    if(door.levelDoor > 0) then
        if(item.aid == 189) then
            if(not isPremium(cid)) then
                doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Only the worthy may pass.")
                return true
            end

            return doorEnter(cid, item.uid, door.transformUseTo, toPosition)
        end

        local gender = item.aid - 186
        if(isInArray({PLAYERSEX_FEMALE,  PLAYERSEX_MALE}, gender)) then
            if(gender ~= getPlayerSex(cid)) then
                doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Only the worthy may pass.")
                return true
            end

            return doorEnter(cid, item.uid, door.transformUseTo, toPosition)
        end

        local skull = item.aid - 180
        if(skull >= SKULL_NONE and skull <= SKULL_BLACK) then
            if(skull ~= getCreatureSkullType(cid)) then
                doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Only the worthy may pass.")
                return true
            end

            return doorEnter(cid, item.uid, door.transformUseTo, toPosition)
        end

        local group = item.aid - 150
        if(group >= 0 and group < 30) then
            if(group > getPlayerGroupId(cid)) then
                doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Only the worthy may pass.")
                return true
            end

            return doorEnter(cid, item.uid, door.transformUseTo, toPosition)
        end

        local vocation = item.aid - 100
        if(vocation >= 0 and vocation < 50) then
            local vocationEx = getVocationInfo(getPlayerVocation(cid))
            if(vocationEx.id ~= vocation and vocationEx.fromVocation ~= vocation) then
                doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Only the worthy may pass.")
                return true
            end

            return doorEnter(cid, item.uid, door.transformUseTo, toPosition)
        end

        if(item.aid == 190 or (item.aid ~= 0 and getPlayerLevel(cid) >= (item.aid - door.levelDoor))) then
            return doorEnter(cid, item.uid, door.transformUseTo, toPosition)
        end

        doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Only the worthy may pass.")
        return true
    end

    if(door.specialDoor) then
        if(item.aid == 100 or (item.aid ~= 0 and getCreatureStorage(cid, item.aid) > 0)) then
            return doorEnter(cid, item.uid, door.transformUseTo, toPosition)
        end

        doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "The door seems to be sealed against unwanted intruders.")
        return true
    end

    toPosition.stackpos = STACKPOS_TOP_MOVEABLE_ITEM_OR_CREATURE
    local fields, thing = getTileItemsByType(fromPosition, ITEM_TYPE_MAGICFIELD), getThingFromPosition(toPosition)
    if(item.uid ~= thing.uid and thing.itemid >= 100 and table.maxn(fields) ~= 0) then
        return true
    end

    local doorCreature = getThingFromPosition(toPosition)
    if(doorCreature.itemid ~= 0) then
        toPosition.x = toPosition.x + 1
        local query = doTileQueryAdd(doorCreature.uid, toPosition, 20) -- allow to stack outside doors, but not on teleports or floor changing tiles
        if(query == RETURNVALUE_NOTPOSSIBLE) then
            toPosition.x = toPosition.x - 1
            toPosition.y = toPosition.y + 1
            query = doTileQueryAdd(doorCreature.uid, toPosition, 20) -- repeat until found
        end

        if(query ~= RETURNVALUE_NOERROR) then
            doPlayerSendDefaultCancel(cid, query)
            return true
        end

        doTeleportThing(doorCreature.uid, toPosition)
        if(not door.closingDoor) then
            doTransformItem(item.uid, door.transformUseTo)
        end

        return true
    end
  
    if(isInArray(keys, item.itemid)) then
        if(itemEx.actionid > 0) then
            if(item.actionid == itemEx.actionid and doors[itemEx.itemid] ~= nil) then
                doTransformItem(itemEx.uid, doors[itemEx.itemid])
                return true
            end

            doPlayerSendCancel(cid, "The key does not match.")
            return true
        end

        return false
    end
  
    if(doors[item.itemid] ~= nil and checkStackpos(item, fromPosition)) then
        if(item.actionid == 0) then
            doTransformItem(item.uid, doors[item.itemid])
        else
            doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "It is locked.")
        end

        return true
    end

    return false
end


and there is error when I'm trying to open doors by key:
doors.png


my 050-function.lua:
http://pastebin.com/4C3ZXFMf

No idea.. it should works but it does not :( does anyone could help me?
 
Line 117
Code:
  if(isInArray(keys, item.itemid)) then
You are missing the table keys.
Code:
keys = {2086, 2087, 2088, 2089, 2090, 2091, 2092, 10032, 10091}
You can add it in constant.lua.
 
Well, I've added it to lib/000-constant.lua :)
My doors.lua:
Code:
local function doorEnter(cid, uid, id, position)
    doTransformItem(uid, id)
    doTeleportThing(cid, position)
    return true
end

function onUse(cid, item, fromPosition, itemEx, toPosition)
    if(fromPosition.x ~= CONTAINER_POSITION and isPlayerPzLocked(cid) and getTileInfo(fromPosition).protection) then
        doPlayerSendDefaultCancel(cid, RETURNVALUE_NOTPOSSIBLE)
        return true
    end

    local locked = DOORS[item.itemid]
    if(locked) then
        doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "It is locked.")
        return true
    end

    local door = getItemInfo(item.itemid)
    if(door.levelDoor > 0) then
        if(item.aid == 189) then
            if(not isPremium(cid)) then
                doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Only the worthy may pass.")
                return true
            end

            return doorEnter(cid, item.uid, door.transformUseTo, toPosition)
        end

        local gender = item.aid - 186
        if(isInArray({PLAYERSEX_FEMALE,  PLAYERSEX_MALE}, gender)) then
            if(gender ~= getPlayerSex(cid)) then
                doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Only the worthy may pass.")
                return true
            end

            return doorEnter(cid, item.uid, door.transformUseTo, toPosition)
        end

        local skull = item.aid - 180
        if(skull >= SKULL_NONE and skull <= SKULL_BLACK) then
            if(skull ~= getCreatureSkullType(cid)) then
                doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Only the worthy may pass.")
                return true
            end

            return doorEnter(cid, item.uid, door.transformUseTo, toPosition)
        end

        local group = item.aid - 150
        if(group >= 0 and group < 30) then
            if(group > getPlayerGroupId(cid)) then
                doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Only the worthy may pass.")
                return true
            end

            return doorEnter(cid, item.uid, door.transformUseTo, toPosition)
        end

        local vocation = item.aid - 100
        if(vocation >= 0 and vocation < 50) then
            local vocationEx = getVocationInfo(getPlayerVocation(cid))
            if(vocationEx.id ~= vocation and vocationEx.fromVocation ~= vocation) then
                doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Only the worthy may pass.")
                return true
            end

            return doorEnter(cid, item.uid, door.transformUseTo, toPosition)
        end

        if(item.aid == 190 or (item.aid ~= 0 and getPlayerLevel(cid) >= (item.aid - door.levelDoor))) then
            return doorEnter(cid, item.uid, door.transformUseTo, toPosition)
        end

        doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "Only the worthy may pass.")
        return true
    end

    if(door.specialDoor) then
        if(item.aid == 100 or (item.aid ~= 0 and getCreatureStorage(cid, item.aid) > 0)) then
            return doorEnter(cid, item.uid, door.transformUseTo, toPosition)
        end

        doPlayerSendTextMessage(cid, MESSAGE_EVENT_ADVANCE, "The door seems to be sealed against unwanted intruders.")
        return true
    end

    toPosition.stackpos = STACKPOS_TOP_MOVEABLE_ITEM_OR_CREATURE
    local fields, thing = getTileItemsByType(fromPosition, ITEM_TYPE_MAGICFIELD), getThingFromPosition(toPosition)
    if(item.uid ~= thing.uid and thing.itemid >= 100 and table.maxn(fields) ~= 0) then
        return true
    end

    local doorCreature = getThingFromPosition(toPosition)
    if(doorCreature.itemid ~= 0) then
        toPosition.x = toPosition.x + 1
        local query = doTileQueryAdd(doorCreature.uid, toPosition, 20) -- allow to stack outside doors, but not on teleports or floor changing tiles
        if(query == RETURNVALUE_NOTPOSSIBLE) then
            toPosition.x = toPosition.x - 1
            toPosition.y = toPosition.y + 1
            query = doTileQueryAdd(doorCreature.uid, toPosition, 20) -- repeat until found
        end

        if(query ~= RETURNVALUE_NOERROR) then
            doPlayerSendDefaultCancel(cid, query)
            return true
        end

        doTeleportThing(doorCreature.uid, toPosition)
        if(not door.closingDoor) then
            doTransformItem(item.uid, door.transformUseTo)
        end

        return true
    end

    if(isInArray(keys, item.itemid)) then
        if(itemEx.actionid > 0) then
            if(item.actionid == itemEx.actionid and doors[itemEx.itemid] ~= nil) then
                doTransformItem(itemEx.uid, doors[itemEx.itemid])
                return true
            end

            doPlayerSendCancel(cid, "The key does not match.")
            return true
        end

        return false
    end

    if(doors[item.itemid] ~= nil and checkStackpos(item, fromPosition)) then
        if(item.actionid == 0) then
            doTransformItem(item.uid, doors[item.itemid])
        else
            doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "It is locked.")
        end

        return true
    end

    return false
end


Now when I'm trying to use key with AID 4601 on door with AID 4601 then I see "You cannot use this object", and error in console:
doors2.png


Also when I'm opening/closing door I see error:
doors3.png


What's wrong?
 
Missing table doors.
Code:
doors = {[1209] = 1211, [1210] = 1211, [1212] = 1214, [1213] = 1214, [1219] = 1220, [1221] = 1222, [1231] = 1233, [1232] = 1233, [1234] = 1236, [1235] = 1236, [1237] = 1238, [1239] = 1240, [1249] = 1251, [1250] = 1251, [1252] = 1254, [1253] = 1254, [1539] = 1540, [1541] = 1542, [3535] = 3537, [3536] = 3537, [3538] = 3539, [3544] = 3546, [3545] = 3546, [3547] = 3548, [4913] = 4915, [4914] = 4915, [4916] = 4918, [4917] = 4918, [5082] = 5083, [5084] = 5085, [5098] = 5100, [5099] = 5100, [5101] = 5102, [5107] = 5109, [5108] = 5109, [5110] = 5111, [5116] = 5118, [5117] = 5118, [5119] = 5120, [5125] = 5127, [5126] = 5127, [5128] = 5129, [5134] = 5136, [5135] = 5136, [5137] = 5139, [5138] = 5139, [5140] = 5142, [5141] = 5142, [5143] = 5145, [5144] = 5145, [5278] = 5280, [5279] = 5280, [5281] = 5283, [5282] = 5283, [5284] = 5285, [5286] = 5287, [5515] = 5516, [5517] = 5518, [5732] = 5734, [5733] = 5734, [5735] = 5737, [5736] = 5737, [6192] = 6194, [6193] = 6194, [6195] = 6197, [6196] = 6197, [6198] = 6199, [6200] = 6201, [6249] = 6251, [6250] = 6251, [6252] = 6254, [6253] = 6254, [6255] = 6256, [6257] = 6258, [6795] = 6796, [6797] = 6798, [6799] = 6800, [6801] = 6802, [6891] = 6893, [6892] = 6893, [6894] = 6895, [6900] = 6902, [6901] = 6902, [6903] = 6904, [7033] = 7035, [7034] = 7035, [7036] = 7037, [7042] = 7044, [7043] = 7044, [7045] = 7046, [7054] = 7055, [7056] = 7057, [8541] = 8543, [8542] = 8543, [8544] = 8546, [8545] = 8546, [8547] = 8548, [8549] = 8550, [9165] = 9167, [9166] = 9167, [9168] = 9170, [9169] = 9170, [9171] = 9172, [9173] = 9174, [9267] = 9269, [9268] = 9269, [9270] = 9272, [9271] = 9272, [9273] = 9274, [9275] = 9276, [10276] = 10277, [10274] = 10275, [10268] = 10270, [10269] = 10270, [10271] = 10273, [10272] = 10273, [10471] = 10472, [10480] = 10481, [10477] = 10479, [10478] = 10479, [10468] = 10470, [10469] = 10470, [10774] = 10776, [10775] = 10776, [10779] = 10780, [10781] = 10782, [10783] = 10785, [10784] = 10785, [10788] = 10789, [10790] = 10791}
You can also add it in 000-constant.lua.
 
I've added it :)
I had problem with closing doors because there were error:
doors4.png


and I've added to doors.lua:
Code:
local function checkStackpos(item, position)
    position.stackpos = STACKPOS_TOP_MOVEABLE_ITEM_OR_CREATURE
    local thing = getThingFromPos(position)

    position.stackpos = STACKPOS_TOP_FIELD
    local field = getThingFromPos(position)

    return (item.uid == thing.uid or thing.itemid < 100 or field.itemid == 0)
end

It seems that it works... Thanks! Do you think everything is ok?
 
Back
Top Bottom