• 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 how to get player by id

Solution
it's just variable assignment. I want to use the player's ID to access other data such as name and position. in addevent you can't pass the entire player object and that's why you need to get to it by id

You just need to pass the player id as a parameter to addEvent and then recreate the Player object inside the addEvent, it will return nil if the player is no longer online or dead...
If the data exists, you can then access the Player object just as usual
Lua:
local function doSomething(playerId)
    local player = Player(playerId)
    if not player then
        return
    end
  
    local name = player:getName()
end

local action = Action()
function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)...
it's just variable assignment. I want to use the player's ID to access other data such as name and position. in addevent you can't pass the entire player object and that's why you need to get to it by id
 
it's just variable assignment. I want to use the player's ID to access other data such as name and position. in addevent you can't pass the entire player object and that's why you need to get to it by id

You just need to pass the player id as a parameter to addEvent and then recreate the Player object inside the addEvent, it will return nil if the player is no longer online or dead...
If the data exists, you can then access the Player object just as usual
Lua:
local function doSomething(playerId)
    local player = Player(playerId)
    if not player then
        return
    end
  
    local name = player:getName()
end

local action = Action()
function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    addEvent(doSomething, 1000, player:getId())
    return true
end
action:id(12345)
action:register()
 
Solution
work perfectyly :) thanx so much. can you help me in other problem? how to in this srcipt check if thing is uh? i must get item id? how to get it? i can't find solution

function countUH(player)

ilosc uh = 0
ilosc manas = 0

for slot = 1, 10 do
local thing = spectator:getSlotItem(player)
if thing then
local copiedItem = thing:clone()
depot:addItemEx(copiedItem, INDEX_WHEREEVER, FLAG_NOLIMIT)
thing:remove()
end
end

end
 
You didn't provide much info so I am not sure exactly what you want...

This will just loop through your 10 slot items and check for runes...
but its pointless...

Lua:
local uhIDs = {2160, 2161}
local manaIDs = {2162, 2163}

function countUH(player)

    local uhs = 0
    local manas = 0
    
    local depot = player:getDepotChest(0)
    if not depot then
        return
    end

    for slot = 1, 10 do
        local item = player:getSlotItem(slot)
        if item then
            item:moveTo(depot)
            uhs = table.contains(uhIDs, item:getId()) and uhs + 1 or uhs
            manas = table.contains(manaIDs, item:getId()) and manas + 1 or manas
        end
    end
    
    --print(uhs)
    --print(manas)
    --return ?
end

What you would need to do is, get the backpack slot or whatever container you want, and loop through all the items (deep, recursively)
 
local uhIDs = {2273, 2273}
local manaIDs = {2162, 2163}

function counterSupples(player)

local uhs = 0
local manas = 0

for slot = 1, 10 do
local item = player:getSlotItem(slot)
if item then
uhs = table.contains(uhIDs, item:getId()) and uhs + 1 or uhs
manas = table.contains(manaIDs, item:getId()) and manas + 1 or manas
end
end

print(uhs)
--print(manas)
--return ?
end

thanx for help but its no count uhs. i have in bp some uhs id 2273 and still print 0
 
thanx for help but its no count uhs. i have in bp some uhs id 2273 and still print 0
please re-read my whole reply....
Post automatically merged:

This will loop through all of your slot items, and backpack items.
All items will be moved to depot. (is this necessary?)
It returns two integers, first is uh count, second is mana count.

Lua:
local uhIDs = {2160, 2161}
local manaIDs = {2162, 2163}

function countUH(player)

    local backpack = player:getSlotItem(3)
    if not backpack or not backpack:isContainer() then
        return 0, 0
    end
 
    local items = backpack:getItems(true)
    if not items then
        return 0, 0
    end
 
    local uhs = 0
    local manas = 0
    for _, item in ipairs(items) do
        uhs = table.contains(uhIDs, item:getId()) and uhs + 1 or uhs
        manas = table.contains(manaIDs, item:getId()) and manas + 1 or manas
    end
 
    return uhs, manas
end

--[[
example usage:

local uhCount, manaCount = countUH(player)
print(uhCount)
print(manaCount)
--]]
 
Last edited:
you wrote: and loop through all the items (deep, recursively)

Po prostu nie wiem jak to zrobić. Czy do każdego plecaka muszę zrobić pętlę?

he doesn't have to transfer it to the depot. I pasted a similar script from my server.
Post automatically merged:

i have this error:
lua:150: attempt to call method 'getItems' (a nil value)
stack traceback:
[C]: in function 'getItems'
 
you wrote: and loop through all the items (deep, recursively)

Po prostu nie wiem jak to zrobić. Czy do każdego plecaka muszę zrobić pętlę?

he doesn't have to transfer it to the depot. I pasted a similar script from my server.
Post automatically merged:

i have this error:
lua:150: attempt to call method 'getItems' (a nil value)
stack traceback:
[C]: in function 'getItems'
In that case, i've edited my previous script, which will just fetch the count...

Check that slot 3 is SLOT_BACKPACK, you could even just use the enum. According to 1.4 it is...

Do you have the following method in your luascript.cpp?

and are you sure there is a container in your backpack slot?
 
register Method("House", "getItems", LuaScriptInterface::luaHouseGetItems);
I also have one for house. Unfortunately, I don't have one for container. Is there any way to bypass this?
 
I need to go out now, but I'll carry on helping you out later on if no-one else does.

Basically, a recursive function will need to be built. It's pretty easy but I need to go.

List your container methods from your luascript.cpp here...
Like this:
Lua:
    // Container
    registerClass("Container", "Item", LuaScriptInterface::luaContainerCreate);
    registerMetaMethod("Container", "__eq", LuaScriptInterface::luaUserdataCompare);

    registerMethod("Container", "getSize", LuaScriptInterface::luaContainerGetSize);
    registerMethod("Container", "getCapacity", LuaScriptInterface::luaContainerGetCapacity);
    registerMethod("Container", "getEmptySlots", LuaScriptInterface::luaContainerGetEmptySlots);
    registerMethod("Container", "getItems", LuaScriptInterface::luaContainerGetItems);
    registerMethod("Container", "getItemHoldingCount", LuaScriptInterface::luaContainerGetItemHoldingCount);
    registerMethod("Container", "getItemCountById", LuaScriptInterface::luaContainerGetItemCountById);

    registerMethod("Container", "getItem", LuaScriptInterface::luaContainerGetItem);
    registerMethod("Container", "hasItem", LuaScriptInterface::luaContainerHasItem);
    registerMethod("Container", "addItem", LuaScriptInterface::luaContainerAddItem);
    registerMethod("Container", "addItemEx", LuaScriptInterface::luaContainerAddItemEx);
    registerMethod("Container", "getCorpseOwner", LuaScriptInterface::luaContainerGetCorpseOwner);
 
// Container
registerClass("Container", "Item", LuaScriptInterface::luaContainerCreate);
registerMetaMethod("Container", "__eq", LuaScriptInterface::luaUserdataCompare);

registerMethod("Container", "getSize", LuaScriptInterface::luaContainerGetSize);
registerMethod("Container", "getCapacity", LuaScriptInterface::luaContainerGetCapacity);
registerMethod("Container", "getEmptySlots", LuaScriptInterface::luaContainerGetEmptySlots);

registerMethod("Container", "getItemHoldingCount", LuaScriptInterface::luaContainerGetItemHoldingCount);
registerMethod("Container", "getItemCountById", LuaScriptInterface::luaContainerGetItemCountById);

registerMethod("Container", "getItem", LuaScriptInterface::luaContainerGetItem);
registerMethod("Container", "hasItem", LuaScriptInterface::luaContainerHasItem);
registerMethod("Container", "addItem", LuaScriptInterface::luaContainerAddItem);
registerMethod("Container", "addItemEx", LuaScriptInterface::luaContainerAddItemEx);

i try by getItem but no work too
maybe getItemCountById be work?
Post automatically merged:

getItemCountById works for me :) thanks for help
 
Last edited:
i try by getItem but no work too
maybe getItemCountById be work?
Post automatically merged:

getItemCountById works for me :) thanks for help
Just double check that it's recursive (I can't remember if it is....)
Does it count all items inside a backpack, inside another backpack etc?
If so... great :)
 
ah actually doesn't count if uh is inside another backpack/ how to do a deep search?
Add this to your data/lib/core/container.lua:
(could be written cleaner but it should work)
Lua:
function Container.getItems(self, recursive, itemArr)
    itemArr = itemArr or {}
 
    for i = 0, self:getSize() - 1 do
        local item = self:getItem(i)
        if not item then
            break
        end
 
        table.insert(itemArr, item)
        if item:isContainer() then
            itemArr = item:getItems(true, itemArr)
        end
    end
 
    return itemArr
end

My previous script should then work fine...

However, I would recommend updating your TFS so you have the lua script methods readily available in the sources.

Be careful that you don't call this function too often.... a high amount of players that have a lot of items each executing this can add up to a lot of computation in a short amount of time.

---


Any more issues will require you to create a separate thread, unless its related to the original question.
 
Last edited:
Back
Top