You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Lua遍历数组遇nil值中断计算问题求助

Fixing Nil Element Interruption in Your Lua Distance/Angle Script

Hey there! I see you're running into an issue where your script stops processing once it hits a nil element in your botallx, botally, or botallz arrays. Let's break down what's going wrong and fix it step by step.

What's Causing the Interruption?

The main problem is that when you pass a nil value to readFloat(), it throws an error that stops your entire loop (and script) cold. Your current if check uses or, which means even if one of the values exists, it tries to run the calculations—including trying to read from a nil address, which crashes the loop. Also, you've got a typo: you defined vecX/vecZ but tried to use calcX/calcZ in your math functions, which would also cause errors.

Modified Script That Skips Nil Elements

Here's the fixed version of your code that skips nil entries and keeps processing the rest of the array:

me = {'["game.exe"]+2A]','["game.exe"]+A8]','["game"]+6A8]'}
botallx = {'["game.exe"]+01]','["game.exe"]+02]','["game.exe"]+03]', '["game.exe"]+04]','["game.exe"]+05]','["game.exe"]+06]','["game.exe"]+07]', '["game.exe"]+08]','["game.exe"]+09]','["game.exe"]+10]','["game.exe"]+11]', '["game.exe"]+12]'}
botally = {'["game.exe"]+31]','["game.exe"]+32]','["game.exe"]+33]', '["game.exe"]+34]','["game.exe"]+35]','["game.exe"]+36]','["game.exe"]+37]', '["game.exe"]+38]','["game.exe"]+39]','["game.exe"]+30]','["game.exe"]+31]', '["game.exe"]+32]'}
botallz = {'["game.exe"]+51]','["game.exe"]+52]','["game.exe"]+53]', '["game.exe"]+54]','["game.exe"]+55]','["game.exe"]+56]','["game.exe"]+57]', '["game.exe"]+58]','["game.exe"]+59]','["game.exe"]+50]','["game.exe"]+51]', '["game.exe"]+52]'}

function sight_angle()
    if (isKeyPressed(VK_E)) then
        for i=1, 12 do
            -- First, check if ALL three values for this index are non-nil
            local xAddr = botallx[i]
            local yAddr = botally[i]
            local zAddr = botallz[i]
            
            if xAddr ~= nil and yAddr ~= nil and zAddr ~= nil then
                -- Calculate vectors using the correct variable names
                local vecX = readFloat(me[1]) - readFloat(xAddr)
                local vecY = readFloat(me[2]) - readFloat(yAddr)
                local vecZ = readFloat(me[3]) - readFloat(zAddr)
                
                -- Fix variable name typos (vecX/vecZ instead of calcX/calcZ)
                local distance = math.sqrt(math.pow(vecZ, 2) + math.pow(vecX, 2))
                local angleX = math.deg(math.atan2(vecZ, vecX))
                
                print("current distance is " .. distance)
                print("current angle is " .. angleX)
            else
                print("Skipping index " .. i .. " due to nil address")
                -- The loop will automatically continue to the next index here
            end
        end
    end
end

Key Changes Explained

  • Strict Nil Check: We now check if xAddr, yAddr, and zAddr are all non-nil using and instead of or. This ensures we only run calculations when we have valid addresses for all three axes—no more passing nil to readFloat().
  • Variable Typo Fix: Swapped calcX/calcZ with vecX/vecZ to match the variables you defined earlier.
  • Local Variables: Added local to variables inside the loop for better performance and scope management (a good Lua practice).
  • Clearer Skip Message: The else block now tells you which index is being skipped, making it easier to debug which entries are nil.

With these changes, your loop will keep running even after hitting a nil element—processing all valid entries in your arrays without interruption.

内容的提问来源于stack exchange,提问作者sasa ha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:11:04