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

How i can check item.id count?

Devlinn

Member
Joined
Mar 25, 2015
Messages
295
Reaction score
6
hello, how i can check item.id count from players?
like this
if item.itemid(cid, 2000,10) then
do something
end

in npc scripts
 
Solution
thanks
can you little explain how to use?
i use tfs 1.2

so if player:getItemCount(itemid) == 2000,10 then
do something
end
doesnt work showing error expect near '.'
you're using it wrong
player:getItemCount(itemid) you're supposed to replace itemid with 2000 (assuming that's the id you're checking)
then the function returns the item count as it says
you need to check if the value returned is equal to 10
so
if player:getItemCount(itemid) == 10 then
but if you want to make it so they have at least 10, not exactly 10 you use >= (greater than or equal to)
thanks
can you little explain how to use?
i use tfs 1.2

so if player:getItemCount(itemid) == 2000,10 then
do something
end
doesnt work showing error expect near '.'
you're using it wrong
player:getItemCount(itemid) you're supposed to replace itemid with 2000 (assuming that's the id you're checking)
then the function returns the item count as it says
you need to check if the value returned is equal to 10
so
if player:getItemCount(itemid) == 10 then
but if you want to make it so they have at least 10, not exactly 10 you use >= (greater than or equal to)
 
Solution
you're using it wrong
player:getItemCount(itemid) you're supposed to replace itemid with 2000 (assuming that's the id you're checking)
then the function returns the item count as it says
you need to check if the value returned is equal to 10
so
if player:getItemCount(itemid) == 10 then
but if you want to make it so they have at least 10, not exactly 10 you use >= (greater than or equal to)
thanks my mistake

@Static_
can you tell me how i can use removeitem with count?
i cant find here any threads about how to use functions (like example)
 
Lua:
-- Remove 1 gold coin
player:removeItem(2148, 1)

-- Remove 10 gold coins
player:removeItem(2148, 10)

-- Remove all gold coins the player has
player:removeItem(2148, player:getItemCount(2148))
 
Sorry for posting this late, but is there a way to ignore the player equipments in the count function? Thanks!
Should've made a new post ...

ignore the player equipments in the count function
Do you mean for example if the player has 5 demon armors in backpack and 1 in armor slot exclude the one they have in the armor slot?
 

Alright, here's how I managed to do it (wrote as talkaction to test it):
Give it a try and make a new post if you are unsure how to use it for what you need or face errors.
( Tested on TFS 1.3 )

@rechdan fixed.

Lua:
local countItems = TalkAction("/countitem")
function countItems.onSay(player, word, param)

    itemId = tonumber(param)
    if not itemId then
        player:sendCancelMessage("Invalid parameter.")
        return false
    end

    local excludeAmount = 0
    for i = CONST_SLOT_HEAD, CONST_SLOT_AMMO do
        -- skip checking backpack (?)
        if i ~= CONST_SLOT_BACKPACK then
            local slotItem = player:getSlotItem(i)
            if slotItem and slotItem:getId() == itemId then
                excludeAmount = excludeAmount + 1
            end
        end
    end

    if slotItem and slotItem:getId() == itemId then
        excludeAmount = excludeAmount + 1
    end

    local itemcount = player:getItemCount(itemId) - excludeAmount
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "player has " .. itemcount .. "x of ".. ItemType(itemId):getName())

    return false
end

countItems:separator(" ")
countItems:register()
 
Last edited:
Try this:

Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local targetItemId = 2494 --Demon Armor ID

    local totalItemCount = player:getItemCount(targetItemId)

    local equipedItemCount = 0
    for slot = CONST_SLOT_HEAD, CONST_SLOT_AMMO do
        local itemInSlot = player:getSlotItem(slot)
        if itemInSlot and itemInSlot:getId() == targetItemId then
            equipedItemCount = equipedItemCount + 1
        end
    end

    local desiredCount = totalItemCount - equipedItemCount

    print("Player have: "..totalItemCount.." in total.")
    print("Player have: "..equipedItemCount.." equiped.")
    print("Player have: "..desiredCount.." in backpack.")

    return true
end

This is a 2017 thread, make a new thread to you. I noticed now...


@Snavy You are right, my bad. I was writing a code that checked all items in backpack recursively, and changed my mind, forgot to change the functions.
 
Last edited:
Back
Top