Attempt to perform arithmetic on a nil value (field ‘ammo’)

Im getting this error ‘’ Attempt to perform arithmetic on a nil value (field ‘ammo’) ‘’ when i press on ‘use’ on the ammobox i have in my inventory. Thanks in advance.

Here is the line there the error shows me to.
‘’ HoldingItem[‘Data’].ammo = math.floor(HoldingItem[‘Data’].ammo + tonumber(Ammo)) ‘’

Hey :slightly_smiling_face: !
The error is pretty litteral, it means that you try to perform an arithmetic action (addition in your case) on a variable that is nil, wich cannot be done. In you case the field ammo of HoldingItem['Data'] is equal to nil : HoldingItem[‘Data’].ammo = nil (to visualize)

Alright, so in what way can i fix this to work like i want it to work ? :stuck_out_tongue:

For that I’ll need to see the entire script ^^

Sent you a DM

can you send me also the solution?

nill value in ammo

table.insert(elements, {

            label = weapon.label .. ' [' .. weapon.ammo .. ']',

            type  = 'item_weapon',

            value = weapon.name,

            ammo  = weapon.ammo

        })

    end

thx

can you help with the sollution of nill value ammo?
esx property normal script in :slight_smile:

table.insert(elements, {

            label = weapon.label .. ' [' .. weapon.ammo .. ']',

            type  = 'item_weapon',

            value = weapon.name,

            ammo  = weapon.ammo

        })

    end

would be nice if you share your fix here. People will find this post and look for help but the solution is not here or there is little help.

Kinda reviving dead post, simply put, the problem is in updating the table to contain some value:

Data["Value"].ammo = Math.floor(Data["Value"].ammo+tonumber(Ammo)) --Data["Value"].ammo == nil
if not Data["Value"].ammo then
    Data["Value"].ammo = 0
end
Ammo = Ammo or 0
Data["Value"].ammo = Math.floor(Data["Value"].ammo+tonumber(Ammo))

Here is a solution to this problem, sometimes table is just nil. Otherwise problem is with event.

1 Like