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

Lua Check items in bp

Lais Prad

Disgusting Scammer
Joined
Apr 12, 2017
Messages
153
Solutions
6
Reaction score
15
Does anyone know a function to check if the player has X items in the backpack or hands or ammo slot?
If possible, inArray .. since we will check several items.

tfs 1.3
 
Solution
How to check items on slot ammo and hands too? thanks

Sorry, didn't see it.
LUA:
local items = {
    2160,
}
local function checkItems(playerUid, items)
    local player = Player(playerUid)
    if not player then return false end
    for i = 1, #items do
        if player:getItemCount(items[i]) > 0 then
        npcHandler:say('Remove the items first.', cid)
        else
        npcHandler:say('This is good.', cid)
        end
    end
    return true
end
LUA:
local items = {{itemid1, count}, {itemid2, count}}
local function checkItems(player)
    for i = 1, #items do
        if not player:hasItem(items[i][1], items[i][2]) then
            return false
        end
    end
    return true
end
 
LUA:
local items = {{itemid1, count}, {itemid2, count}}
local function checkItems(player)
    for i = 1, #items do
        if not player:hasItem(items[i][1], items[i][2]) then
            return false
        end
    end
    return true
end

I need without count, how I can do this?
LUA:
if player:getItem(itemId) or player:getItem(itemId) or player:getItem(itemId) then
end
 
just set the count to 1
LUA:
local config = {
    {id = 2160, count = 1},
}

local function checkItems(playerUid, items)
    local player = Player(playerUid)
    if not player then return false end
  
    for i = 1, #items do
        if player:getItemCount(items[i].id) < items[i].count then
            return false
        end
    end
    return true
end
 
just set the count to 1
LUA:
local config = {
    {id = 2160, count = 1},
}

local function checkItems(playerUid, items)
    local player = Player(playerUid)
    if not player then return false end
 
    for i = 1, #items do
        if player:getItemCount(items[i].id) < items[i].count then
            return false
        end
    end
    return true
end

but this function work inside script npc.lua ?
I tested here and nothing happens

LUA:
local config = {
    {id = 2160, count = 1},
}

local function checkItems(playerUid, items)
    local player = Player(playerUid)
    if not player then return false end
    for i = 1, #items do
        if player:getItemCount(items[i].id) < items[i].count then
        npcHandler:say('Remove the items first.', cid)
       
        else
        npcHandler:say('This is good.', cid)
       
        end
    end
    return true
end
 
but this function work inside script npc.lua ?
I tested here and nothing happens

Change it to:

LUA:
local config = {
    2160,
}

local function checkItems(playerUid, items)
    local player = Player(playerUid)
    if not player then return false end
    for i = 1, #items do
        if player:getItemCount(items[i]) < items[i].count then
        npcHandler:say('Remove the items first.', cid)
   
        else
        npcHandler:say('This is good.', cid)
   
        end
    end
    return true
end

LUA:
local config = {
    2160,
}
local function checkItems(playerUid, items)
    local player = Player(playerUid)
    if not player then return false end
    for i = 1, #items do
        if player:getItemCount(items[i]) > 0 then
        npcHandler:say('Remove the items first.', cid)
   
        else
        npcHandler:say('This is good.', cid)
   
        end
    end
    return true
end
 
LUA:
local config = {
    2160,
}
local function checkItems(playerUid, items)
    local player = Player(playerUid)
    if not player then return false end
    for i = 1, #items do
        if player:getItemCount(items[i]) > 0 then
        npcHandler:say('Remove the items first.', cid)
 
        else
        npcHandler:say('This is good.', cid)
 
        end
    end
    return true
end

How to check items on slot ammo and hands too? thanks
 
How to check items on slot ammo and hands too? thanks

Sorry, didn't see it.
LUA:
local items = {
    2160,
}
local function checkItems(playerUid, items)
    local player = Player(playerUid)
    if not player then return false end
    for i = 1, #items do
        if player:getItemCount(items[i]) > 0 then
        npcHandler:say('Remove the items first.', cid)
        else
        npcHandler:say('This is good.', cid)
        end
    end
    return true
end
 
Solution
Sorry, didn't see it.
LUA:
local items = {
    2160,
}
local function checkItems(playerUid, items)
    local player = Player(playerUid)
    if not player then return false end
    for i = 1, #items do
        if player:getItemCount(items[i]) > 0 then
        npcHandler:say('Remove the items first.', cid)
        else
        npcHandler:say('This is good.', cid)
        end
    end
    return true
end
nop :p but, how we can check items on slot ammo and hands?

280319b109f24034bb8c03a3317fd5ea.png
 
nop :p but, how we can check items on slot ammo and hands?

280319b109f24034bb8c03a3317fd5ea.png

You want to check ONLY these two slots? Because otherwise, this script will check all player items, including these slots. If you want to only check these two slots, you can use Player:getSlotItem(SLOT) and compare to the item you want to find.
 
Don't make a mess in the function, you can use it for other scripts later. Keep it like this:
LUA:
function checkItems(playerUid, items)
    local player = Player(playerUid)
    if not player then return false end
    for i = 1, #items do
        if player:getItemCount(items[i].id) < items[i].count then
            return false
        end
    end
    return true
end

and use in npc script like:
LUA:
local requiredItems= {
    {id = 2160, count = 1},
}
if checkItems(cid, requiredItems) then
    npcHandler:say('Remove the items first.', cid)
else
    npcHandler:say('This is good.', cid)
end
 

Similar threads

Back
Top