gpt4 book ai didi

Lua - 尝试调用全局(nil 值)

转载 作者:行者123 更新时间:2023-12-01 19:39:14 25 4
gpt4 key购买 nike

执行此代码时,我收到错误消息“尝试调用全局‘forId’(一个 nil 值)”

function execute(args)
local itemid = 526
local bone = forId(itemid) -- this is where the error occurs
end

function forId(bid)
local xp = 0.0
if bid == 526 or bid == 528 or bid == 2530 or bid == 2859 then
xp = 4.5
elseif bid == 3179 or bid == 3180 or bid == 3183 or bid == 3185 then
xp = 5.0
elseif bid == 530 then
xp = 53
elseif bid == 532 or bid == 3125 then
xp = 15
elseif bid == 4812 then
xp = 22.5
elseif bid == 7839 then
xp = 30
elseif bid == 6812 then
xp = 50
elseif bid == 536 then
xp = 72
end
local bone = Bone:new(bid, xp)
return bone
end

Bone = class(function(b, id, xp)
b.id = id
b.xp = xp
end)

谁能告诉我为什么?

最佳答案

Lua 逐行处理和执行文件,因此您定义和使用它们的顺序可能很重要。在这种情况下,虽然看起来您没有提供所有代码,因为看起来您正在定义 forID作为全局,但错误暗示否则。您可以简单地尝试更改函数定义的顺序,看看它是否有效。

Bone = class(function(b, id, xp)
b.id = id
b.xp = xp
end)

function forId(bid)
local xp = 0.0
if bid == 526 or bid == 528 or bid == 2530 or bid == 2859 then
xp = 4.5
elseif bid == 3179 or bid == 3180 or bid == 3183 or bid == 3185 then
xp = 5.0
elseif bid == 530 then
xp = 53
elseif bid == 532 or bid == 3125 then
xp = 15
elseif bid == 4812 then
xp = 22.5
elseif bid == 7839 then
xp = 30
elseif bid == 6812 then
xp = 50
elseif bid == 536 then
xp = 72
end
local bone = Bone:new(bid, xp)
return bone
end

function execute(args)
local itemid = 526
local bone = forId(itemid) -- this is where the error occurs
end

但是由于您没有提供完整的代码,这可能只会导致错误转移到其他地方。

关于Lua - 尝试调用全局(nil 值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5694331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com