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

Lua addEvent: Remove created item

Codinablack

Dreamer
Content Editor
Joined
Dec 26, 2013
Messages
2,122
Solutions
14
Reaction score
1,516
Location
USA
GitHub
Codinablack
Hi Otland. I created a spell to conjure up a trap in the spot the player is standing and I can't figure out how to get this exact item to disappear after so many seconds using addEvent. I just want it to make sure if the trap was set or not that the trap does indeed disappear after so many seconds.

Here is the script:


Code:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_RED)

function onCastSpell(cid, var)
local creature = Creature(cid)
local pos = creature:getPosition()
    doCreateItem(2579, 1, pos)
    combat:execute(cid, var)
    return true
end
 
Code:
local function removeTrap(position, itemId)
    local trapItem = Tile(position):getItemById(itemId)
    if trapItem then
        trapItem:remove()
    end
end

addEvent(removeTrap, time, position, itemId)

btw, the first parameter for onCastSpell is a Creature userdata :p
 
Don't I need to use a unique Id to make sure it removes that trap only?
And also if the trap gets set, then the itemid gets changed, but I want it to still remove the item..
 
I tried like this, and I get error for creature being nil value on line [local pos = creature:getPosition()] but it places the trap, I am guessing that's because my position isn't constant, so I need to set the position for wherever the item gets created, here is the script I have now....

Code:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_RED)

local function removeTrap(position, itemId)
local creature = Creature(cid)
local pos = creature:getPosition()
    local trapItem = Tile(pos):getItemById(2579)
    if trapItem then
        trapItem:remove()
    end
end

function onCastSpell(cid, var)
local creature = Creature(cid)
local pos = creature:getPosition()
    doCreateItem(2579, 1, pos)
    combat:execute(cid, var)
    addEvent(removeTrap, 2000, pos, 2579)
    return true
end
 
Code:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_RED)

local function removeTrap(pos, itemId)
     local trapItem = Tile(pos):getItemById(itemId)
     if trapItem then
         trapItem:remove()
     end
end

function onCastSpell(creature, var)
     local pos = creature:getPosition()
     doCreateItem(2579, 1, pos) 
     combat:execute(creature, var)
     addEvent(removeTrap, 2000, pos, 2579)
     return true
end
 
Code:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_RED)

local function removeTrap(pos, itemId)
     local trapItem = Tile(pos):getItemById(2579)
     if trapItem then
         trapItem:remove()
     end
end

function onCastSpell(creature, var)
     local pos = creature:getPosition()
     doCreateItem(2579, 1, pos)
     combat:execute(creature, var)
     addEvent(removeTrap, 2000, pos, 2579)
     return true
end

Ok so your way works except now if the trap gets used then it causes error, so I tried this

Code:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_RED)

local function removeTrap(pos, itemId)
     local trapItem = Tile(pos):getItemById(2579)
    local trapItem2 = Tile(pos):getItemById(2578)
     if trapItem then
         trapItem:remove()
     end
    if trapItem2 then
        trapItem:remove()
    end
end

function onCastSpell(creature, var)
     local pos = creature:getPosition()
     doCreateItem(2579, 1, pos)
     combat:execute(creature, var)
     addEvent(removeTrap, 2000, pos, 2579)
     return true
end

And it fails, for removing the trap if its been used

NEVER MIND FOUND THE PROBLEM
Forgot to add 2 after trapItem on the second remove
 
Code:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_RED)

local function removeTrap(pos, itemId)
     local tile = Tile(pos)
     local trapItem, trapItem2 = tile:getItemById(itemId), tile:getItemById(itemId - 1)
     if trapItem then
         trapItem:remove()
     elseif trapItem2 then
         trapItem2:remove()
     end
end

function onCastSpell(creature, var)
     local pos = creature:getPosition()
     doCreateItem(2579, 1, pos)  
     combat:execute(creature, var)
     addEvent(removeTrap, 2000, pos, 2579)
     return true
end

Edit: didn't saw your edit :p
 
Last edited:
Code:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_RED)

local function removeTrap(pos, itemId)
     local tile = Tile(pos)
     local trapItem, trapItem2 = tile:getItemById(itemId), tile:getItemById(itemId - 1)
     if trapItem then
         trapItem:remove()
     elseif trapItem2 then
         trapItem2:remove()
     end
end

function onCastSpell(creature, var)
     local pos = creature:getPosition()
     doCreateItem(2579, 1, pos) 
     combat:execute(creature, var)
     addEvent(removeTrap, 2000, pos, 2579)
     return true
end

Edit: didn't saw your edit :p

Yes I tried to do like you said and leave for others to learn from... Okay so I tried advancing the script, and I keep running into problems. I have tried multiple ways around this, but it seems like I can't use player functions since I have parameter for creature idk... Here is the script, this is best I could figure out how to do it, but it returns nil on the first player meta method
Code:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_RED)

local function removeTrap(pos, itemId)
     local tile = Tile(pos)
     local trapItem, trapItem2 = tile:getItemById(2579), tile:getItemById(2578 - 1)
     if trapItem then
         trapItem:remove()
        player:setStorageValue(44501,(player:getStorageValue(44501) - 1))
     elseif trapItem2 then
         trapItem2:remove()
        player:setStorageValue(44501,(player:getStorageValue(44501) - 1))
     end
end

function onCastSpell(creature, cid, var)
     local pos = creature:getPosition()
    local player = Player(cid)
    if player:getStorageValue(44501) < 0 then
    player:setStorageValue(44501, 0)
    end
    if player:getStorageValue(44501) < 3 then
     doCreateItem(2579, 1, pos)
    player:setStorageValue(44501,(player:getStorageValue(44501) + 1))
    combat:execute(player, var)
     addEvent(removeTrap, 2000, pos, 2579)
     return true
    else
    if player:getStorageValue(44501) >= 3 then
    player:sendTextMessage(MSG_INFO_SMALL, "You can only have three traps at a time")
    return false
    end     
end
end
 
What is really odd is if I replace player: with creature: then the storage value will add to my player, but it doesn't take away and it returns error too for nil value
 
Code:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_RED)

local function removeTrap(cid, position, itemId)
    local player = Player(cid)
    if player then
        player:setStorageValue(44501, (player:getStorageValue(44501) - 1))
    end

    local tile = Tile(position)
    local trapItem, trapItem2 = tile:getItemById(itemId), tile:getItemById(itemId - 1)
    if trapItem then
        trapItem:remove()
    elseif trapItem2 then
        trapItem2:remove()
    end
end

function onCastSpell(creature, var)
    if creature:getStorageValue(44501) == 3 then
        creature:sendCancelMessage("You can only have three traps at a time")
        return false
    end

    local pos = creature:getPosition()
    Game.createItem(2579, 1, pos)
    addEvent(removeTrap, 5000, creature:getId(), pos, 2579)
    creature:setStorageValue(44501, math.max(0, creature:getStorageValue(44501)) + 1)
    combat:execute(creature, var)
    return true
end
 
Code:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_RED)

local function removeTrap(cid, position, itemId)
    local player = Player(cid)
    if player then
        player:setStorageValue(44501, (player:getStorageValue(44501) - 1))
    end

    local tile = Tile(position)
    local trapItem, trapItem2 = tile:getItemById(itemId), tile:getItemById(itemId - 1)
    if trapItem then
        trapItem:remove()
    elseif trapItem2 then
        trapItem2:remove()
    end
end

function onCastSpell(creature, var)
    if creature:getStorageValue(44501) == 3 then
        creature:sendCancelMessage("You can only have three traps at a time")
        return false
    end

    local pos = creature:getPosition()
    Game.createItem(2579, 1, pos)
    addEvent(removeTrap, 5000, creature:getId(), pos, 2579)
    creature:setStorageValue(44501, math.max(0, creature:getStorageValue(44501)) + 1)
    combat:execute(creature, var)
    return true
end
Ok so I copied your script and everything worked like its supposed to, can't put out too many traps, After one removes am allowed to place another, it removes the trap if its been used or not, works perfectly all the way around except I get one error
Code:
Lua Script Error: [MoveEvents Interface]
data/movements/scripts/trap.lua:onRemoveItem
data/movements/scripts/trap.lua:28: attempt to index local 'trap' (a nil value)
stack traceback:
        [C]: ?
        data/movements/scripts/trap.lua:28: in function <data/movements/scripts/
trap.lua:26>
        [C]: in function 'remove'
        data/spells/scripts/attack/trap.lua:13: in function <data/spells/scripts
/attack/trap.lua:4>

Here is the script.

Code:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_MAGIC_RED)

local function removeTrap(cid, position, itemId)
    local player = Player(cid)
    if player then
        player:setStorageValue(44501, (player:getStorageValue(44501) - 1))
    end

    local tile = Tile(position)
    local trapItem, trapItem2 = tile:getItemById(2579), tile:getItemById(2579 - 1)
    if trapItem then
        trapItem:remove()
    elseif trapItem2 then
        trapItem2:remove()
    end
end

function onCastSpell(creature, var)
    if creature:getStorageValue(44501) == 3 then
        creature:sendCancelMessage("You can only have three traps at a time")
        return false
    end

    local pos = creature:getPosition()
    Game.createItem(2579, 1, pos)
    addEvent(removeTrap, 5000, creature:getId(), pos, 2579)
    creature:setStorageValue(44501, math.max(0, creature:getStorageValue(44501)) + 1)
    combat:execute(creature, var)
    return true
end
 
Go to movements, trap.lua, then change this part.
Code:
function onRemoveItem(item, tile, position)
     local trap = Item(item.uid)
     if trap then
         local thingPos = trap:getPosition()
         if getDistanceBetween(thingPos, position) > 0 then
             trap:transform(item.itemid - 1)
             thingPos:sendMagicEffect(CONST_ME_POFF)
         end
     end
     return true
end
 
Back
Top