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

Solved [1.0] function onAddItem (cid)

Aleada

Unknown Member
Joined
Mar 14, 2013
Messages
231
Reaction score
7
Heya everyone, I'm wondering if there is any (cid) function in LUA that targets a specific player, not everyone online... If that makes sense xd but also, I'm using TFS 1.0.

For example:
Code:
function onAddItem(moveitem, tileitem, position)
You can't include
Code:
, cid

I've found this code on the forums:
Code:
for _, cid in pairs(getOnlinePlayers()) do

But it just sends the message to everyone :/ Not just the person who throws the item...
Thank you in advance for reading this!

Here's my script:
Code:
function onAddItem(moveitem, tileitem, position)
    if moveitem.itemid == 2579 then
    local storage, amount = 0, 1
        for _, cid in pairs(getOnlinePlayers()) do
        local player = Player(cid)
            player:sendTextMessage(MESSAGE_INFO_DESCR, "TEST")
        end
    end
end

Thank you again!
 
You can use player events I guess. ( data/events/ )
Enable onMoveItem in the xml file and replace the code. Not tested.

Code:
function Player:onMoveItem(item, count, fromPosition, toPosition)
  if item:getId() == 2579 then
    print(self:getName() .. ' moved item 2579.')
  end

  return true
end
 
@Summ Hey I've been messing with it a little while but I'm not sure if I'm doing it right..
This is what I've wrote so far but I don't think it's picking up the Unique ID of the tile :/
Code:
function Player:onMoveItem(item, count, fromPosition, toPosition)
local pos =
{
    [40002] = {x = 549, y = 1496, z = 8},
    [40001] = {x = 585, y = 1480, z = 8},
    [40000] = {x = 601, y = 1490, z = 8},
    [40003] = {x = 608, y = 1470, z = 9},
    [40004] = {x = 573, y = 1501, z = 9}
}

    if pos[item:getTile().uid] ~= nil then
        if item:getId() == 2579 and item:getPosition() == pos[item:getTile().uid] then
            print("Success!")
        end
    else
        print("Fail.")
    end
    return true
end

Here's one that goes just by positions but it's getting the "fromPosition" instead of "toPosition" :x Got any ideas?
Code:
function Player:onMoveItem(item, count, fromPosition, toPosition)
local pos =
{
    [40002] = {x = 549, y = 1496, z = 8},
    [40001] = {x = 585, y = 1480, z = 8},
    [40000] = {x = 601, y = 1490, z = 8},
    [40003] = {x = 608, y = 1470, z = 9},
    [40004] = {x = 573, y = 1501, z = 9}
}
local loop = 40000
    if item:getId() == 2579 then   
        for testing = 1,5 do
            if item:getPosition().x == pos[loop].x and item:getPosition().y == pos[loop].y and item:getPosition().z == pos[loop].z then
                print("WOOOT")
            else
                print("FAIL")
            end
            loop = loop + 1
            print(loop)
        end
    end
    return true
end
 
Last edited by a moderator:
Try with this,
Code:
function Player:onMoveItem(item, count, fromPosition, toPosition)
    local pos = {
        [40000] = {x = 601, y = 1490, z = 8},
        [40001] = {x = 585, y = 1480, z = 8},
        [40002] = {x = 549, y = 1496, z = 8},
        [40003] = {x = 608, y = 1470, z = 9},
        [40004] = {x = 573, y = 1501, z = 9}
    }

    if item:getId() == 2579 then
        local tile = toPosition:getTile()
        if tile then
            local ground = tile:getGround()
            if ground then
                local targetTile = pos[ground:getUniqueId()]
                if not targetTile then
                    return true
                end
             
                if toPosition.x == targetTile.x and toPosition.y == targetTile.y and toPosition.z == targetTile.z then
                    print("Success!")
                end
            end
        end
    end
    return true
end
 
Thank you both for helping! Sorry I didn't see your message before I solved it :/ This is what the finished product looks like though if anyone else wants to learn off of it:
Code:
function Player:onMoveItem(item, count, fromPosition, toPosition)
local pos =
{
    [40002] = {x = 549, y = 1496, z = 8},
    [40001] = {x = 585, y = 1480, z = 8},
    [40000] = {x = 601, y = 1490, z = 8},
    [40003] = {x = 608, y = 1470, z = 9},
    [40004] = {x = 573, y = 1501, z = 9}
}
local tile = toPosition:getTile()
    if tile then
    local tile_uid = tile:getGround():getAttribute(ITEM_ATTRIBUTE_UNIQUEID)
    local tile_pos = tile:getPosition()
    local storage, amount = 0, 1

        if pos[tile_uid] ~= nil then
            if item:getId() == 2579 then
                if tile_pos.x == pos[tile_uid].x and tile_pos.y == pos[tile_uid].y and tile_pos.z == pos[tile_uid].z then
                    if self:getStorageValue(tile_uid) ~= 1 then
                        for testing = 1,5 do
                            if self:getStorageValue(40000 + storage) > 0 then
                                amount = amount + 1
                                if amount == 5 then
                                    self:setStorageValue(20004, 5)
                                end
                            end
                            storage = storage + 1
                        end
                        item:remove()
                        self:setStorageValue(tile_uid, 1)
                        toPosition:sendMagicEffect(46)
                        self:say(amount.." / 5 traps placed.", 1)
                    else
                        toPosition:sendMagicEffect(3)
                        self:say("You have already placed a trap here.", 1)
                    end
                end
            elseif item:getId() == 2578 then
                self:say("It would be a good thing to open it first and then place it.", 1)
            end
        end
    end
    return true
end

Sorry if it's a bit messy xD

Thank you again @Ninja and @Summ
 
Back
Top