[string "GUILayout/NPC/tiandidaoxin.lua"]:125: attempt to compare string with number
local sycee = SL:GetMetaValue("MONEY", 2)
if sycee <= next_cfg.value2 then
-- 元宝不足时显示红色
if GUI:Win_IsNotNull(ui.yuanbao) then
GUI:Text_setString(ui.
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-13 14:43:31 |
👁️ 阅读: 5
错误分析:`SL:GetMetaValue("MONEY", 2)` 返回的是字符串类型,而 `next_cfg.value2` 是数字类型,字符串与数字直接比较会报错。
**解决方案:**
```lua
local syceeStr = SL:GetMetaValue("MONEY", 2)
local sycee = tonumber(syceeStr) or 0 -- 转换为数字,如果转换失败则默认为0
if sycee <= next_cfg.value2 then
-- 元宝不足时显示红色
if GUI:Win_IsNotNull(ui.yuanbao) then
GUI:Text_setString(ui.yuanbao, ": " .. (next_cfg.value2 or 0))
GUI:Text_setTextColor(ui.yuanbao, "#ff0000") -- 红色
end
else
-- 元宝足够时显示绿色
if GUI:Win_IsNotNull(ui.yuanbao) then
GUI:Text_setString(ui.yuanbao, ": " .. (next_cfg.value2 or 0))
GUI:Text_setTextColor(ui.yuanbao, "#5ac83d") -- 绿色
end
end
```
**关键修改:**
1. 使用 `tonumber()` 函数将字符串转换为数字
2. 添加 `or 0` 作为转换失败时的默认值
3. 确保比较双方都是数字类型
**注意:** 浮生梦老师提醒过,从服务端获取的数据大多是字符串类型,进行数值比较前一定要先转换!
错误分析:`SL:GetMetaValue("MONEY", 2)` 返回的是字符串类型,而 `next_cfg.value2` 是数字类型,字符串与数字直接比较会报错。
**解决方案:**
```lua
local syceeStr = SL:GetMetaValue("MONEY", 2)
local sycee = tonumber(syceeStr) or 0 -- 转换为数字,如果转换失败则默认为0
if sycee <= next_cfg.value2 then
-- 元宝不足时显示红色
if GUI:Win_IsNotNull(ui.yuanbao) then
GUI:Text_setString(ui.yuanbao, ": " .. (next_cfg.value2 or 0))
GUI:Text_setTextColor(ui.yuanbao, "#ff0000") -- 红色
end
else
-- 元宝足够时显示绿色
if GUI:Win_IsNotNull(ui.yuanbao) then
GUI:Text_setString(ui.yuanbao, ": " .. (next_cfg.value2 or 0))
GUI:Text_setTextColor(ui.yuanbao, "#5ac83d") -- 绿色
end
end
```
**关键修改:**
1. 使用 `tonumber()` 函数将字符串转换为数字
2. 添加 `or 0` 作为转换失败时的默认值
3. 确保比较双方都是数字类型
**注意:** 浮生梦老师提醒过,从服务端获取的数据大多是字符串类型,进行数值比较前一定要先转换!
← 返回问答库