LUA:尝试索引本地”(nil值)

时间:2019-09-28 00:01:20

标签: lua

请帮助:我正在尝试为本地firstClipArr(一个nil值)建立索引,但是我不知道该如何解决这个问题。这是代码:

function updateRemoteAmmo( ammoTable )
if not client or getElementData(client,"loggedin") ~= 1 then
    return
end
local items = getItems( client )
local weaponCheck = { }
for _, itemCheck in ipairs( items ) do
    if (itemCheck[1] == 115) then -- Weapon item
        local weaponItemDetails = explode(':', itemCheck[2])
        local weaponAmmoCountValid = 0
        local firstClip = 0
        local firstClipInventorySlot = 0
        local firstClipArr = nil


        for itemCheckBulletsSlot, itemCheckBullets in ipairs( items ) do
            if (itemCheckBullets[1] == 116) then 
                local weaponBulletDetails = explode(':', itemCheckBullets[2])
                if tonumber(weaponBulletDetails[1]) == tonumber(weaponItemDetails[1]) then
                    if (firstClip == 0 and tonumber(weaponBulletDetails[2]) > 0) then
                        firstClip =  tonumber(weaponBulletDetails[2])
                        firstClipInventorySlot = itemCheckBulletsSlot
                        firstClipArr = weaponBulletDetails
                    end

                    weaponAmmoCountValid = weaponAmmoCountValid + weaponBulletDetails[2]
                end
            end
        end


        local weaponSyncPacket = false
        for _, tableValue in ipairs(ammoTable) do

            if tonumber(tableValue[1]) == tonumber(weaponItemDetails[1]) then

                weaponSyncPacket = tableValue
            end
        end

        if not weaponSyncPacket then

            return
        end


        local fakebullet = false
        if (getElementData(client, "cf:"..weaponItemDetails[1])) then
            fakebullet = true
            weaponAmmoCountValid = weaponAmmoCountValid + 1
        end


        if (weaponAmmoCountValid > weaponSyncPacket[2]) then -- There has been shot with this gun

            local totalBulletsShot = weaponAmmoCountValid - weaponSyncPacket[2]

            local update = updateItemValue(client, firstClipInventorySlot,firstClipArr[1] ..":"..weaponSyncPacket[3]  ..":"..  firstClipArr[3])

            if not update then
                outputDebugString("fail")
            end
            weaponAmmoCountValid = weaponAmmoCountValid - totalBulletsShot
        end

        table.insert(weaponCheck, {weaponItemDetails[1], weaponAmmoCountValid, fakebullet} )
    end
end

0 个答案:

没有答案