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

Lua Small problem with "getTarget" in a new function

Oneda

Aspiring Spriter
Joined
Dec 3, 2013
Messages
159
Solutions
2
Reaction score
104
Location
Brazil
So guys, How should I get the player target inside a new function I'm making? (Target isnt a creature, should be a tile/item)

LUA:
function isGathered(cid, decayId)
    for _, tPlayer in ipairs(Game.getPlayers()) do
    local target = cid:getTarget()
        target:transform(decayId)
        target:decay()
        return true
    end
end

Console shows me this error:
Code:
data/lib/compat/compat.lua:121: attempt to index local 'cid' (a number value)
stack traceback:
        [C]: in function '__index'
        data/lib/compat/compat.lua:121: in function 'isGathered'
        data/actions/scripts/gather.lua:6: in function <data/actions/scripts/gather.lua:3>
 
Solution
Alright guys I solved my problem, thanks everyone! The final function is:

LUA:
function isGathered(targetPos, targetId)
    local tile = Tile(targetPos)
    if tile then
        local item = tile:getItemById(targetId)
        if item and item.itemid == 2767 then
            item:transform(2784)
            item:decay()
            targetPos:sendMagicEffect(35)
        elseif item and item.itemid == 2768 then
            item:transform(2770)
            item:decay()
            targetPos:sendMagicEffect(35)
        end
    end
    return true
end
So guys, How should I get the player target inside a new function I'm making? (Target isnt a creature, should be a tile/item)

LUA:
function isGathered(cid, decayId)
    for _, tPlayer in ipairs(Game.getPlayers()) do
    local target = cid:getTarget()
        target:transform(decayId)
        target:decay()
        return true
    end
end

Console shows me this error:
Code:
data/lib/compat/compat.lua:121: attempt to index local 'cid' (a number value)
stack traceback:
        [C]: in function '__index'
        data/lib/compat/compat.lua:121: in function 'isGathered'
        data/actions/scripts/gather.lua:6: in function <data/actions/scripts/gather.lua:3>
With Game.getPlayers() you're returning a table with all online players. Then when you do 'for _, tPlayers' _ is the order number of the table your looping and tPlayer is the Player data of the current loop.

But after the problem line you still have issues. transform and decay functions are for items and cid:getTarget() is a player or monster so that wont work either.
 
Last edited:
There are a few ways to do this loop.

LUA:
-- Loop through known numeric indexes
local table = { "A", "B", "C", "D" }
for k = 1, #table do
  print(table[k])
end

LUA:
-- Loop from index 1 to 'n' (n is the last index of table). Only when keys are numeric and start from 1 to 'n' as your table
local table = { "A", "B", "C", "D" }
for key, value in ipairs(table) do
  print(value)
end

LUA:
-- Randomly loop, when the order doesn't matters. It will includes the ones with numeric keys and string keys, no matter if it has jumps on the numeric keys, no matter if the numeric keys is negative or has the key 0, or string as the keys.
local table = { "A", "B", "C", "D" }
for key, value in pairs(table) do
  print(value)
end

Note: The "_" on like "for _, value in ipairs(table) do" is just a var. This is the SAME as "for key, value in ipairs(table) do" and also the SAME as "for key, _ in ipairs(table) do". But "_" is common used meaning that you will not use the key or the value if one of them are "_".


Also, I didn't understand what you want to do on your code.
 
what do you mean by target being an item?
creature:getTarget() is a monster or player, the thing the creature is attacking, not an item.
i don't know how you're expecting to get some item from the player without using player:getSlotItem(slot) or player:getItemById(id)
 
what do you mean by target being an item?
creature:getTarget() is a monster or player, the thing the creature is attacking, not an item.
i don't know how you're expecting to get some item from the player without using player:getSlotItem(slot) or player:getItemById(id)

Uhh, im making an action, which uses onUse target parameter, all I need to know is how to do that in a new function im making.

Will read other comments on PC soon, but till now thanks for the help guys.
 
You can pass "target:getPosition()" and "target:getId()" or just position and use getThing() on target tile position.

Code:
local function xD(targetPos, targetId)
    local tile = Tile(targetPos)
    if tile then
        local item = tile:getItemById(targetId)
        if item then
            --
        end
    end
    return true
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if target:isItem() then
        xD(target:getPosition(), target:getId())
    end
    return true
end

I'm not sure if you can pass whole "target" to new function.
 
Last edited:
You can pass "target:getPosition()" and "target:getId()" or just position and use getThing() on target tile position.

Code:
local function xD(targetPos, targetId)
    local tile = Tile(targetPos)
    if tile then
        local item = tile:getItemById(targetId)
        if item then
            --
        end
    end
    return true
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if target:isItem() then
        xD(target:getPosition(), target:getId())
    end
    return true
end

I'm not sure if you can pass whole "target" to new function.

You can pass the whole target to new function.
You cannot pass it only through addEvent, because target wouldn't a safe value.
Like this:

LUA:
local function xD(target)
    local tile = Tile(target:getPosition())
    if tile then
        local item = tile:getItemById(target:getId())
        if item then
            --
        end
    end
    return true
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if target:isItem() then
        xD(target)

        -- If you needed this function as addEvent, you could use like that:
        -- (and yes, you can use as event even the function is local)
        --[[
          addEvent(function(cid)
            local target = Creature(cid)
            if not target then return end
            xD(target)
          end,
          0, target:getId())
        ]]
    return true
end
 
You can pass the whole target to new function.
You cannot pass it only through addEvent, because target wouldn't a safe value.
Like this:

LUA:
local function xD(target)
    local tile = Tile(target:getPosition())
    if tile then
        local item = tile:getItemById(target:getId())
        if item then
            --
        end
    end
    return true
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if target:isItem() then
        xD(target)

        -- If you needed this function as addEvent, you could use like that:
        -- (and yes, you can use as event even the function is local)
        --[[
          addEvent(function(cid)
            local target = Creature(cid)
            if not target then return end
            xD(target)
          end,
          0, target:getId())
        ]]
    return true
end
edited:nevermind im retarded
 
Alright guys I solved my problem, thanks everyone! The final function is:

LUA:
function isGathered(targetPos, targetId)
    local tile = Tile(targetPos)
    if tile then
        local item = tile:getItemById(targetId)
        if item and item.itemid == 2767 then
            item:transform(2784)
            item:decay()
            targetPos:sendMagicEffect(35)
        elseif item and item.itemid == 2768 then
            item:transform(2770)
            item:decay()
            targetPos:sendMagicEffect(35)
        end
    end
    return true
end
 
Solution
Back
Top