• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Remove all items from a pos

Paulix

Active Member
Joined
Sep 13, 2012
Messages
151
Solutions
8
Reaction score
36
I have the following script, that blocks a certain area once a player goes in
LUA:
function onStepIn(cid, item, pos)
    pos1 = {x=1119, y=874, z=8, stackpos=1}
    pos2 = {x=1122, y=873, z=8, stackpos=1}
    pos3= {x=1124, y=875, z=8, stackpos=1}

    rock1 = getThingfromPos(pos1)
    rock2 = getThingfromPos(pos2)
    rock3 = getThingfromPos(pos3)

    if item.actionid == 20000 then

        --if player
            --moveplayer
        --end

        doRemoveItem(rock1.uid, 1)

        newrock1 = doCreateItem(3608, 1, pos1)
    end

    if item.actionid == 20001 then

        --if player
            --moveplayer
        --end

        doRemoveItem(rock2.uid, 1)

        newrock1 = doCreateItem(3608, 1, pos2)
    end

    if item.actionid == 20002 then

        --if player
            --moveplayer
        --end

        doRemoveItem(rock3.uid, 1)

        newrock1 = doCreateItem(3608, 1, pos3)
    end
end

function onStepOut(cid, item, pos)

    pos1 = {x=1119, y=874, z=8, stackpos=1}
    pos2 = {x=1122, y=873, z=8, stackpos=1}
    pos3= {x=1124, y=875, z=8, stackpos=1}

    rock1 = getThingfromPos(pos1)
    rock2 = getThingfromPos(pos2)
    rock3 = getThingfromPos(pos3)

    if item.actionid == 20000 then

        doRemoveItem(rock1.uid, 1)

        newrock1 = doCreateItem(3610, 1, pos1)
    end

    if item.actionid == 20001 then

        doRemoveItem(rock2.uid, 1)

        newrock1 = doCreateItem(3610, 1, pos2)
    end

    if item.actionid == 20002 then

        doRemoveItem(rock3.uid, 1)

        newrock1 = doCreateItem(3610, 1, pos3)
    end
end
but if someone throws something above the walkable rock, it remove this item instead, and when it creates another rock it gets 2, making it glitch the next time someone steps in. I would like to remove all items from that pos before creating the rock to block the path. Server is tfs 0.4.3
 
Solution
Code:
-- By @The_Pain
function removeAllItemsFromPos(pos, protectItemId)
    for index = 1, 255 do
        pos.stackpos = index
        local thing = getThingfromPos(pos)
        if thing.itemid > 0 and thing.itemid ~= protectItemId then
            doRemoveItem(thing.uid)
        end
    end
    return true
end

local positions = {
    { x = 1119, y = 874, z = 8 },
    { x = 1122, y = 873, z = 8 },
    { x = 1124, y = 875, z = 8 }
}
local returnPosition = {
    { x = 1120, y = 876, z = 8 }, -- return old pos
    { x = 1120, y = 876, z = 8 }, -- return old pos
    { x = 1120, y = 876, z = 8 } -- return old pos
}
-- By @The_Pain
function getCreaturesFromPos(pos)
    local creatures = {}
    if pos == nil then
        return creatures...
here with the help of a friend.
Code:
local positions = {
    { x = 1119, y = 874, z = 8 },
    { x = 1122, y = 873, z = 8 },
    { x = 1124, y = 875, z = 8 }
}
local returnPosition = { x = 1110, y = 875, z = 8 }

-- By @The_Pain
function tileGetPlayers(position)
local players = {}
if position == nil then
    return players
end
local maxStackpos = 255
for index = 1, maxStackpos do
    position.stackpos = index
    local thing = getThingfromPos(position)
    if isPlayer(thing.uid) then
        table.insert(players, thing)
    end
end
return players
end

-- By @The_Pain
function findThingFromPos(position, itemId)
local maxStackpos = 255
for index = 1, maxStackpos do
    position.stackpos = index
    local thing = getThingfromPos(position)
    if thing.itemid == itemId then
        thing.isItem = true
        return thing
    end
end
return { itemid = 0 }
end

function onStepIn(cid, item, pos)
    for s = 0, 2 do
    if item.actionid == (20000 + s) then
    local thing = findThingFromPos(positions[s + 1], 3610)
    local players = tileGetPlayers(positions[s + 1])
    if #players > 0 then
    for _, player in pairs(players) do
    doTeleportThing(player.uid, returnPosition, true)
    doSendMagicEffect(returnPosition, CONST_ME_TELEPORT)
    end end
    if thing.isItem then
    doRemoveItem(thing.uid)
    doSendMagicEffect(positions[s + 1], CONST_ME_POFF)
    end
    doCreateItem(3608, 1, positions[s + 1])
    end end
return true
end

function onStepOut(cid, item, pos)
    for s = 0, 2 do
    if item.actionid == (20000 + s) then
    local thing = findThingFromPos(positions[s + 1], 3608)
    local players = tileGetPlayers(positions[s + 1])
    if #players > 0 then
    for _, player in pairs(players) do
    doTeleportThing(player.uid, returnPosition, true)
    doSendMagicEffect(returnPosition, CONST_ME_TELEPORT)
    end end
    if thing.isItem then
    doRemoveItem(thing.uid)
    doSendMagicEffect(positions[s + 1], CONST_ME_POFF)
    end
    doCreateItem(3610, 1, positions[s + 1])
    end end
return true
end
 
@Sarah Wesker I made some adjustments to the code to make it work more like I want, here is how it is as now...

LUA:
local positions = {
    { x = 1119, y = 874, z = 8 },
    { x = 1122, y = 873, z = 8 },
    { x = 1124, y = 875, z = 8 }
}
local returnPosition = { x = 1120, y = 876, z = 8 }
-- By @The_Pain
function getCreaturesFromPos(pos)
    local creatures = {}
    if pos == nil then
        return creatures
    end
    for index = 1, 255 do
        pos.stackpos = index
        local thing = getThingfromPos(pos)
        if isCreature(thing.uid) then
            table.insert(creatures, thing)
        end
    end
    return creatures
end
-- By @The_Pain
function getItemsFromPos(pos, itemId)
    for index = 1, 255 do
        pos.stackpos = index
        local thing = getThingfromPos(pos)
        if thing.itemid == itemId then
            thing.isItem = true
            return thing
        end
    end
    return {itemid = 0}
end
function onStepIn(cid, item, pos)
    for s = 0, #positions do
        if item.actionid == (20000 + s) then
            local thing = getItemsFromPos(positions[s + 1], 3610)
            local players = getCreaturesFromPos(positions[s + 1])
            if #players > 0 then
                for _, player in pairs(players) do
                    doTeleportThing(player.uid, returnPosition, true)
                end
            end
            if thing.isItem then
                doRemoveItem(thing.uid)
            end
            doCreateItem(3608, 1, positions[s + 1])
        end
    end
return true
end
function onStepOut(cid, item, pos)
    for s = 0, #positions do
        if item.actionid == (20000 + s) then
            local thing = getItemsFromPos(positions[s + 1], 3608)
            if thing.isItem then
                doRemoveItem(thing.uid)
            end
            doCreateItem(3610, 1, positions[s + 1])
        end
    end
    return true
end
As you can see below it is working, but when recreating a rock it is making it above the items.
[gif]
I would like it or to remove all items on the tile, or make the stone reappears below all items.
And the player shouldnt be teleported, it should be moved like the this.
XlVEXz5.png

I don't think we need the getCreatureFromPos function to get "all" creatures, since it can only have 1 creature at time, so if you move that creature it should be fine.
Here is how it is working right now.
[gif]
 
Code:
-- By @The_Pain
function removeAllItemsFromPos(pos, protectItemId)
    for index = 1, 255 do
        pos.stackpos = index
        local thing = getThingfromPos(pos)
        if thing.itemid > 0 and thing.itemid ~= protectItemId then
            doRemoveItem(thing.uid)
        end
    end
    return true
end

local positions = {
    { x = 1119, y = 874, z = 8 },
    { x = 1122, y = 873, z = 8 },
    { x = 1124, y = 875, z = 8 }
}
local returnPosition = {
    { x = 1120, y = 876, z = 8 }, -- return old pos
    { x = 1120, y = 876, z = 8 }, -- return old pos
    { x = 1120, y = 876, z = 8 } -- return old pos
}
-- By @The_Pain
function getCreaturesFromPos(pos)
    local creatures = {}
    if pos == nil then
        return creatures
    end
    for index = 1, 255 do
        pos.stackpos = index
        local thing = getThingfromPos(pos)
        if isCreature(thing.uid) then
            table.insert(creatures, thing)
        end
    end
    return creatures
end
-- By @The_Pain
function getItemsFromPos(pos, itemId)
    for index = 1, 255 do
        pos.stackpos = index
        local thing = getThingfromPos(pos)
        if thing.itemid == itemId then
            thing.isItem = true
            return thing
        end
    end
    return {itemid = 0}
end
function onStepIn(cid, item, pos)
    for s = 0, #positions do
        if item.actionid == (20000 + s) then
            local thing = getItemsFromPos(positions[s + 1], 3610)
            local players = getCreaturesFromPos(positions[s + 1])
            if #players > 0 then
                for _, player in pairs(players) do
                    doTeleportThing(player.uid, returnPosition[s + 1], true)
                end
            end
            removeAllItemsFromPos(positions[s + 1], 3608)
            if thing.isItem then
                doRemoveItem(thing.uid)
            end
            doCreateItem(3608, 1, positions[s + 1])
        end
    end
return true
end
function onStepOut(cid, item, pos)
    for s = 0, #positions do
        if item.actionid == (20000 + s) then
            local thing = getItemsFromPos(positions[s + 1], 3608)
            removeAllItemsFromPos(positions[s + 1], 3610)
            if thing.isItem then
                doRemoveItem(thing.uid)
            end
            doCreateItem(3610, 1, positions[s + 1])
        end
    end
    return true
end
 
Solution
@Sarah Wesker I made some adjustments to the code to make it work more like I want, here is how it is as now...

LUA:
local positions = {
    { x = 1119, y = 874, z = 8 },
    { x = 1122, y = 873, z = 8 },
    { x = 1124, y = 875, z = 8 }
}
local returnPosition = { x = 1120, y = 876, z = 8 }
-- By @The_Pain
function getCreaturesFromPos(pos)
    local creatures = {}
    if pos == nil then
        return creatures
    end
    for index = 1, 255 do
        pos.stackpos = index
        local thing = getThingfromPos(pos)
        if isCreature(thing.uid) then
            table.insert(creatures, thing)
        end
    end
    return creatures
end
-- By @The_Pain
function getItemsFromPos(pos, itemId)
    for index = 1, 255 do
        pos.stackpos = index
        local thing = getThingfromPos(pos)
        if thing.itemid == itemId then
            thing.isItem = true
            return thing
        end
    end
    return {itemid = 0}
end
function onStepIn(cid, item, pos)
    for s = 0, #positions do
        if item.actionid == (20000 + s) then
            local thing = getItemsFromPos(positions[s + 1], 3610)
            local players = getCreaturesFromPos(positions[s + 1])
            if #players > 0 then
                for _, player in pairs(players) do
                    doTeleportThing(player.uid, returnPosition, true)
                end
            end
            if thing.isItem then
                doRemoveItem(thing.uid)
            end
            doCreateItem(3608, 1, positions[s + 1])
        end
    end
return true
end
function onStepOut(cid, item, pos)
    for s = 0, #positions do
        if item.actionid == (20000 + s) then
            local thing = getItemsFromPos(positions[s + 1], 3608)
            if thing.isItem then
                doRemoveItem(thing.uid)
            end
            doCreateItem(3610, 1, positions[s + 1])
        end
    end
    return true
end
As you can see below it is working, but when recreating a rock it is making it above the items.
[gif]
I would like it or to remove all items on the tile, or make the stone reappears below all items.
And the player shouldnt be teleported, it should be moved like the this.
XlVEXz5.png

I don't think we need the getCreatureFromPos function to get "all" creatures, since it can only have 1 creature at time, so if you move that creature it should be fine.
Here is how it is working right now.
[gif]
It took me forever to figure out what you were trying to do lol.
Try to explain exactly what you want to happen in the future. >.<
LUA:
local config = {
    [20001] = {small_rock = 3608, big_rock = 3610, pos = {x = 1119, y = 874, z = 8}},
    [20002] = {small_rock = 3608, big_rock = 3610, pos = {x = 1122, y = 873, z = 8}},
    [20003] = {small_rock = 3608, big_rock = 3610, pos = {x = 1124, y = 875, z = 8}}
}

function onStepIn(cid, item, pos)
    local x = config[item.actionid]
    if x then
        if getTileItemById(x.pos, x.small_rock) > 0 then
            doTransformItem(getTileItemById(x.pos, x.small_rock).uid, big_rock)
        end
    end
    return true
end

function onStepOut(cid, item, pos)
    local x = config[item.actionid]
    if x then
        if getTileItemById(x.pos, x.big_rock) > 0 then
            doTransformItem(getTileItemById(x.pos, x.big_rock).uid, small_rock)
        end
    end
    return true
end
 
@Sarah Wesker it is working flawless now, even tho I have to rewrite almost all the script, you showed me the way to make it. Thank you!
Here is the final script if anyone is interested in
LUA:
local positions = {
    {x = 1119, y = 874, z = 8},
    {x = 1122, y = 873, z = 8},
    {x = 1124, y = 875, z = 8}
}

local movepos = {2, 2, 3}
function getCreatureFromPos(pos)
    if pos == nil then
        return false
    end
    pos.stackpos = 253
    local thing = getThingfromPos(pos)
    if isCreature(thing.uid) then
        return thing
    end
    return false
end
function getItemsOnTile(pos)
    local items = 0
    for i = 1, 255 do
        pos.stackpos = i
        local thing = getThingfromPos(pos)
        if thing.itemid > 0 then
            items = items+1
        end
    end
    return items
end
function onStepIn(cid, item, pos)
    for i = 0, #positions do
        if item.actionid == (20000+i) and isPlayer(cid) then
            local creature = getCreatureFromPos(positions[i+1])
      
            if creature then
                doMoveCreature(creature.uid, movepos[i+1])
            end
            if getItemsOnTile(positions[i+1]) > 1 then
                doSendMagicEffect(positions[i+1], CONST_ME_POFF)
            end
            doCleanTile(positions[i+1])
            doCreateItem(3608, 1, positions[i+1])
        end
    end
    return true
end
function onStepOut(cid, item, pos)
    for i = 0, #positions do
        if item.actionid == (20000+i) then
            doCleanTile(positions[i+1])
            doCreateItem(3610, 1, positions[i+1])
        end
    end
    return true
end
@Xikini I added some poff effects when some item is destroyed by the rock, your code looks way cleaner than mine, but my script is working and I'm pretty proud of it. I have some gifs uploaded on my other post, so you can try to figure out what this script is supposed to do. Thanks for the help anyways :D
 
Last edited:
Back
Top