尝试索引全局变量'message'时出错 Lua消息脚本

目前,我正在编写一个简单的Lua Roblox脚本,当任何玩家在聊天中输入"/blue"时,它应该将父部件变成蓝色。运行时,输出中返回错误"attempt to index global 'message' (a nil value)"。当我将鼠标悬停在"message"上时,它会显示"unknown global 'message'"。我相信我做错了什么,因为我是新手。我尝试将脚本移动到Workspace和聊天中(当然要更改当地的部分),但这些都没用。我有信心这是一个代码问题,具体是定义全局变量的问题。

local part = script.Parent

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

scan()
点赞
用户12650168
用户12650168

首先,你没有定义“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)

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

2020-01-04 03:05:43