尝试索引全局 'message'(零值)Lua 消息脚本

attempt to index global 'message' (a nil value) Lua Message script

目前,我正在开发一个简单的 Lua Roblox 脚本,当任何玩家在聊天中输入“/blue”时,该脚本应该将父部分变为蓝色。当 运行 时,它 returns 输出中的错误 "attempt to index global 'message' (a nil value)"。此外,当我将光标悬停在 "message" 上时,它会显示 "unknown global 'message'"。由于我是该语言的新手,因此我确定我做错了很多事。我曾尝试将脚本移至工作区和聊天室(当然,当我这样做时会更改本地部分),但这些都无济于事。我确信这是专门定义全局变量的代码问题。

local part = script.Parent

local function scan()
    if message:sub(1,5) == "/blue" then
        part.BrickColor = BrickColor.Blue()
    end
end

scan()

首先,你没有定义 "message" 因为 "message" 应该是

的参数
player.Chatted()

所以不只是 运行 scan(),而是创建多个函数,这里是修改后的代码:

local part = script.Parent

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(message)
        message = string.lower(message)
        if message == "/blue" then
            part.BrickColor = BrickColor.new("Blue")
        end
    end)
end)

如果您需要我详细说明,请告诉我,我知道有时这些东西会令人困惑。