Skip to content

[BUG] "Cannot hold item" I can't buy items in stores #168

@Kashioliver777

Description

@Kashioliver777

I took a good look at the code and tried to break it down further to look for the error. From what I noticed, for some reason the code cannot identify the player's inventory

--- Checks if an item can be added to a inventory based on the weight and slots available.
--- @param identifier string The identifier of the player or inventory.
--- @param item string The item name.
--- @param amount number The amount of the item.
--- @return boolean - Returns true if the item can be added, false otherwise.
--- @return string|nil - Returns a string indicating the reason why the item cannot be added (e.g., 'weight' or 'slots'), or nil if it can be added.

print ('this is inventory:' ..identifier)
function CanAddItem(identifier, item, amount)
if not Inventories[identifier] then
print('dont seach inventory')
return false
end
local Player = QBCore.Functions.GetPlayer(identifier)
local itemData = QBCore.Shared.Items[item:lower()]
if not itemData then return false end

local inventory, items
if Player then
    inventory = {
        maxweight = Config.MaxWeight,
        slots = Config.MaxSlots
    }
    items = Player.PlayerData.items
    print ('have slots')
elseif Inventories[identifier] then
    inventory = Inventories[identifier]
    items = Inventories[identifier].items
    print ('dont have slots')
end

local weight = itemData.weight * amount
local totalWeight = GetTotalWeight(items) + weight
if totalWeight > inventory.maxweight then
    return false, 'weight'
end

if not inventory or not items then
    print("CanAddItem: Inventory not found")
    return false
end

I added some "prints" for debug

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions