• 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 doRemoveItem help on 1.1 Action

Caduceus

Unknown Member
Joined
May 10, 2010
Messages
321
Solutions
2
Reaction score
24
The switch works correctly as far as creating the steps, the timer resets the switch, but the steps do not get removed on reset. This is for tfs 1.1

Code:
local stairPos = {
   [1] = {x=1120, y=927, z=9, stackpos=1},
   [2] = {x=1120, y=928, z=9, stackpos=1}
}

local function reset(p)
   -- reset switch
  doTransformItem(getTileItemById(p, 1946).uid, 1945)
   -- remove stairs
   doTransformItem(getThingfromPos(stairPos[1]).uid, 0)
   doTransformItem(getThingfromPos(stairPos[2]).uid, 0)
end

function onUse(cid, item, frompos, item2, topos)

   -- check if lever is currently used
   if item.itemid == 1946 then
     return doPlayerSendTextMessage(cid, 22, "Switch is active.")
   end
   -- transform lever, and add reset
   doTransformItem(item.uid, item.itemid + 1)
   addEvent(reset, 5 * 60 * 1000, topos)
   -- create steps, send message and effect
   doCreateItem(8378, 1, stairPos[1])
   doCreateItem(8379, 1, stairPos[2])
   doPlayerSendTextMessage(cid, 22, "Access Granted!")  
   return true
end
 
Last edited:
you should use 1.1 metatable functions like item:remove(),
Code:
local stairPos = {
   [1] = {x=1120, y=927, z=9, stackpos=1},
   [2] = {x=1120, y=928, z=9, stackpos=1}
}

there is no uid in this code and its trying to remove by uid
doRemoveItem(stairPos[1].uid, 1)
doRemoveItem(stairPos[2].uid, 1)

try to put an uid in there
 
@godoyxd they are called metamethods not metatable functions, using the proper terminology for the language is more important than you might expect.

@topic if your going to declare the function local you might as well declare it inside the onUse interface, this way you don't need to concern yourself with the arguments, because the data it will process will be considered global in scope.

Also you don't need to listen to everyone when they tell you to use the metamethods inside of a script, there is a reason there is a compat file.
Lets use doRemoveItem as an example, in compat.lua on line 560 this is the definition of the function:
Code:
function doRemoveItem(uid, ...) local i = Item(uid) return i ~= nil and i:remove(...) or false end

What the definition says is to store the item inside of i using its uid passed to Item, then the return value checks to see if the item exists if does exists it removes the item, if it doesn't exist then it returns false.

If you were to use just item:remove(1) then you would generate an error if the item didn't exist, the problem here is the majority of people who do write code using godoyxd's method run into errors because they don't understand the reasoning behind the compat file or the function definitions.

Code:
    local stairPos = {
       [1] = {x=1120, y=927, z=9, stackpos=1},
       [2] = {x=1120, y=928, z=9, stackpos=1}
    }

    local time_ = 50 -- seconds

    function onUse(player, item, fromPosition, target, toPosition, isHotkey)
        local function reset()
            -- reset switch
            item:transform(1945)
            -- remove stairs
            for _, pos in ipairs(stairPos) do
    ,             doRemoveItem(getTileThingByPos(pos), 1)
            end
        end

       -- check if lever is currently used
       if item.itemid == 1946 then
         player:sendTextMessage(MESSAGE_INFO_DESCR, "Switch is active.")
         return false
       end
   
       -- transform lever, and add reset
       item:transform(1946)
       addEvent(reset, time_ * 1000)
   
       -- create steps, send message and effect
       Game.createItem(8378, 1, stairPos[1])
       Game.createItem(8379, 1, stairPos[2])
       player:sendTextMessage(MESSAGE_INFO_DESCR, "Access Granted!")
       return true
    end
 
Last edited:
Thank you for the explanation, however the items still do not remove from pos. Also get this error when the lever is used.

Code:
Lua Script Error: [Action Interface]
data/actions/scripts/custom/hydra_lever.lua:onUse
data/actions/scripts/custom/hydra_lever.lua:31: attempt to index global 'player'
(a nil value)
stack traceback:
        [C]: in function '__index'
        data/actions/scripts/custom/hydra_lever.lua:31: in function <data/action
s/scripts/custom/hydra_lever.lua:8>
 
Thank you for the explanation, however the items still do not remove from pos. Also get this error when the lever is used.

Code:
Lua Script Error: [Action Interface]
data/actions/scripts/custom/hydra_lever.lua:onUse
data/actions/scripts/custom/hydra_lever.lua:31: attempt to index global 'player'
(a nil value)
stack traceback:
        [C]: in function '__index'
        data/actions/scripts/custom/hydra_lever.lua:31: in function <data/action
s/scripts/custom/hydra_lever.lua:8>
I updated the code 15 minutes ago, prior to the initial post of 45 minutes ago
It was because of the onUse parameters, so try it again and see if you get an error, btw I used the annilator script as a template for the onUse so you should be able to use player straight away without having to use say cid and then convert it to a player userdata

line 8 is this
Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
prior to the update the original post said this
Code:
function onUse(cid, item, frompos, item2, topos)

If there is an error in the script, it won't execute at all that is why the items aren't removed.

Yea I overlook things, but at least I admit it :)
 
Last edited:
still not removing items on time_ & I removed the "," at line 14.
why would you do that?
ok so lets see what getTileThingByPos(pos) is returning, take a screen shot of the console
Code:
        local stairPos = {
           [1] = {x=1120, y=927, z=9, stackpos=1},
           [2] = {x=1120, y=928, z=9, stackpos=1}
        }

        local time_ = 50 -- seconds

        function onUse(player, item, fromPosition, target, toPosition, isHotkey)
            local function reset()
                -- reset switch
                item:transform(1945)
                -- remove stairs
                for _, pos in ipairs(stairPos) do
                    -- for now lets see what getTileThingByPos is returning
                        print( getTileThingByPos(pos) )
        ,             --doRemoveItem(getTileThingByPos(pos), 1)
                end
            end

           -- check if lever is currently used
           if item.itemid == 1946 then
             player:sendTextMessage(MESSAGE_INFO_DESCR, "Switch is active.")
             return false
           end
     
           -- transform lever, and add reset
           item:transform(1946)
           addEvent(reset, time_ * 1000)
     
           -- create steps, send message and effect
           Game.createItem(8378, 1, stairPos[1])
           Game.createItem(8379, 1, stairPos[2])
           player:sendTextMessage(MESSAGE_INFO_DESCR, "Access Granted!")
           return true
        end
 
because I get this error on load
a2akuh.png



with last code, minus ","

Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
            local function reset()
                -- reset switch
                item:transform(1945)
                -- remove stairs
                for _, pos in ipairs(stairPos) do
                    -- for now lets see what getTileThingByPos is returning
                        print( getTileThingByPos(pos) )
        ,             --doRemoveItem(getTileThingByPos(pos), 1)
                end
            end

2renqk8.png
 
Last edited:
ok so now try this
Code:
        local stairPos = {
           [1] = {x=1120, y=927, z=9, stackpos=1},
           [2] = {x=1120, y=928, z=9, stackpos=1}
        }

        local time_ = 50 -- seconds

        function onUse(player, item, fromPosition, target, toPosition, isHotkey)
            local function reset()
                -- reset switch
                item:transform(1945)
                -- remove stairs
                for _, pos in ipairs(stairPos) do
        ,          doRemoveItem(getTileThingByPos(pos).uid)
                end
            end

           -- check if lever is currently used
           if item.itemid == 1946 then
             player:sendTextMessage(MESSAGE_INFO_DESCR, "Switch is active.")
             return false
           end
      
           -- transform lever, and add reset
           item:transform(1946)
           addEvent(reset, time_ * 1000)
      
           -- create steps, send message and effect
           Game.createItem(8378, 1, stairPos[1])
           Game.createItem(8379, 1, stairPos[2])
           player:sendTextMessage(MESSAGE_INFO_DESCR, "Access Granted!")
           return true
        end
 
Back
Top