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

How to make a door that closes after player pass through?

cristianso

Member
Joined
Jan 27, 2019
Messages
47
Reaction score
16
I want to make a script that the player use a KEY to open the door and then closes it automaticaly after he pass through the door.

I tried to make it using an "addEvent" but never close the doors, so I think I am doing something really wrong.
 
TFS version? You can just add a movement script OnStepOut so when players going out of the door it closes it/transforms it to closed door.
Example
Lua:
doTransformItem(item.uid, item.itemid - 1)
 
TFS version? You can just add a movement script OnStepOut so when players going out of the door it closes it/transforms it to closed door.
Example
Lua:
doTransformItem(item.uid, item.itemid - 1)

Yep, it worked! Way better! Thanks.

However, if people open the door from diagonal you may have a problem. So I made this script:

Lua:
function onStepOut(creature, item, position, fromPosition)
local doorPosition = Position(14654, 13582, 10)
local doorId = Tile(doorPosition):getItemById(1222)
local player = creature:getPlayer()
local playerPosition = player:getPosition()

    if not player then
        return true
    end

    local function closeDoor()
        if doorId then
            doorId:transform(1221)
        end
    end

    if playerPosition:getDistance(doorPosition) > 1 then
        addEvent(closeDoor, 100, fromPosition)         
    end

return true
end

It means that if he's far from the door 1sqm, the door is going to close automatically.
I also edited SQMs around the door to have an uid.
 
Last edited:
Yep, it worked! Way better! Thanks.

However, if people open the door from diagonal you may have a problem. So I made this script:

Lua:
function onStepOut(creature, item, position, fromPosition)
local doorPosition = Position(14654, 13582, 10)
local doorId = Tile(doorPosition):getItemById(1222)
local player = creature:getPlayer()
local playerPosition = player:getPosition()

    if not player then
        return true
    end

    local function closeDoor()
        if doorId then
            doorId:transform(1221)
        end
    end

    if playerPosition:getDistance(doorPosition) > 1 then
        addEvent(closeDoor, 100, fromPosition)        
    end

return true
end

It means that if he's far from the door 1sqm, the door is going to close automatically.
I also edited SQMs around the door to have an uid.

Your code works, but has some problems. You need to check if there is another player in the door, move items, etc. Check the closingdoor.lua from the TFS repo:
Lua:
function onStepOut(creature, item, position, fromPosition)
    local tile = Tile(position)
    if tile:getCreatureCount() > 0 then
        return true
    end

    local newPosition = {x = position.x + 1, y = position.y, z = position.z}
    local query = Tile(newPosition):queryAdd(creature)
    if query ~= RETURNVALUE_NOERROR or query == RETURNVALUE_NOTENOUGHROOM then
        newPosition.x = newPosition.x - 1
        newPosition.y = newPosition.y + 1
        query = Tile(newPosition):queryAdd(creature)
    end

    if query == RETURNVALUE_NOERROR or query ~= RETURNVALUE_NOTENOUGHROOM then
        doRelocate(position, newPosition)
    end

    local i, tileItem, tileCount = 1, true, tile:getThingCount()
    while tileItem and i < tileCount do
        tileItem = tile:getThing(i)
        if tileItem and tileItem:getUniqueId() ~= item.uid and tileItem:getType():isMovable() then
            tileItem:remove()
        else
            i = i + 1
        end
    end

    item:transform(item.itemid - 1)
    return true
end

This should work with StepOut using your open door id, the only difference would be the line "item:transform(item.itemid - 1)", replacing with your custom closed door id, supposing that its not the open door id - 1 (if it is, just use the original then).
 
Your code works, but has some problems. You need to check if there is another player in the door, move items, etc. Check the closingdoor.lua from the TFS repo:
Lua:
function onStepOut(creature, item, position, fromPosition)
    local tile = Tile(position)
    if tile:getCreatureCount() > 0 then
        return true
    end

    local newPosition = {x = position.x + 1, y = position.y, z = position.z}
    local query = Tile(newPosition):queryAdd(creature)
    if query ~= RETURNVALUE_NOERROR or query == RETURNVALUE_NOTENOUGHROOM then
        newPosition.x = newPosition.x - 1
        newPosition.y = newPosition.y + 1
        query = Tile(newPosition):queryAdd(creature)
    end

    if query == RETURNVALUE_NOERROR or query ~= RETURNVALUE_NOTENOUGHROOM then
        doRelocate(position, newPosition)
    end

    local i, tileItem, tileCount = 1, true, tile:getThingCount()
    while tileItem and i < tileCount do
        tileItem = tile:getThing(i)
        if tileItem and tileItem:getUniqueId() ~= item.uid and tileItem:getType():isMovable() then
            tileItem:remove()
        else
            i = i + 1
        end
    end

    item:transform(item.itemid - 1)
    return true
end

This should work with StepOut using your open door id, the only difference would be the line "item:transform(item.itemid - 1)", replacing with your custom closed door id, supposing that its not the open door id - 1 (if it is, just use the original then).
Should I do something before running it on my server? Because I just paste this into my script and it did not work. The "transform item" thing is ok, my door is really -1 id.
 
Should I do something before running it on my server? Because I just paste this into my script and it did not work. The "transform item" thing is ok, my door is really -1 id.
Maybe your XML config is wrong, can you show it? And tell me whats the open door id
 
<movevent event="StepOut" uniqueid="20000" script="quests/Secret Treasure/closedoor.lua" />
You should use the open door itemId instead of an unique id.
<movevent event="StepOut" itemid="1224" script="closingdoor.lua" />
Replace 1224 with your open door item ID.
If you use an updated TFS, you probably have already this closingdoor.lua script, and you can delete this closedoor.lua that you created.
 
Back
Top