local ceshi = {}
ceshi.bg[[
<Img|x=31.0|y=97.0|reset=1|bg=1|esc=1|move=0|show=0|loadDelay=1|img=public/1900000675.jpg>
<Layout|x=707.0|y=83.0|width=80|height=80|link=@exit>
<Button|x=728.0|y=97.0|nimg=public/1900000510.png|pimg=public/1900000511
🤖 回答引擎: 后端Lua |
🕒 时间: 2026-04-06 15:03:35 |
👁️ 阅读: 0
后端Lua老师来啦!
小仙,你好呀!看到你的代码和报错信息了,这个问题很典型,让为师来给你指点一下。
首先,报错信息 `attempt to call field 'bg' (a nil value)` 已经告诉我们问题所在了。在你的代码中,`ceshi.bg` 这个字段被定义为一个多行字符串,但是当你在 `main` 函数里通过 `ceshi.bg` 去调用它时,Lua把它当作一个函数(`call`)来执行了,而它实际上是一个字符串,所以报错了。
`say` 函数需要的第一个参数是玩家对象,第二个参数就是一个**字符串**,用来显示对话框内容。你直接把 `ceshi.bg` 这个字符串赋值给 `msg` 再传给 `say` 就行了,不需要去“调用”它。
我们来修复你的 `main` 函数:
```lua
function ceshi:main(actor)
-- 这里直接使用 ceshi.bg 这个字符串变量,它不是一个函数
local msg = ceshi.bg
say(actor, msg)
end
```
但是,等等!为师再仔细一看你的代码结构,发现了一个更根本的问题。你定义 `ceshi.bg` 的方式是 `ceshi.bg[[...]]`,这在Lua中是一个**语法错误**。正确的定义表字段的方法应该是 `ceshi.bg = [[...]]`。
所以,你的整个模块开头应该这样写:
```lua
local ceshi = {}
ceshi.bg = [[
<Img|x=31.0|y=97.0|reset=1|bg=1|esc=1|move=0|show=0|loadDelay=1|img=public/1900000675.jpg>
<Layout|x=707.0|y=83.0|width=80|height=80|link=@exit>
<Button|x=728.0|y=97.0|nimg=public/1900000510.png|pimg=public/1900000511.png|link=@exit>
<Button|x=59.0|y=123.0|size=18|color=255|nimg=public/1900000661.png|text=给技能|link=@jineng>
]]
function ceshi:main(actor)
-- 现在 ceshi.bg 是一个字符串,直接使用
local msg = ceshi.bg
say(actor, msg)
end
-- ... 后面的 jineng, gs, zr 等函数保持不变 ...
return ceshi
```
**总结一下你犯的两个小马虎:**
1. `ceshi.bg[[...]]` 应该是 `ceshi.bg = [[...]]`,少了等号。
2. 在 `main` 函数里,`ceshi.bg` 是字符串,不是函数,不能加括号调用。
按照上面的修改,你的NPC脚本就能正常加载和运行了。以后写代码要细心一点哦,尤其是这种定义变量和调用的时候。如果还有不明白的,随时可以再来问为师,或者请教我师傅浮生梦尊者!
← 返回问答库