roblox studio 'end' 期望(在第1行关闭 'function' 的位置附近)<eof>

我尝试编写一些代码来创建一个名为“Console”的文件夹,当有人聊天“Console on”时,在工作区中,当有人说“Console off”时将其删除。然而,当我在公共游戏中运行它(因为roblox studio的测试模式中没有聊天)时,我得到了标题中的错误,阅读了几篇文章,但没有找到答案。

game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
print("Connected")
if msg == "Console on" then
    console = Instance.new("Folder",workspace)
    console.name = "Console"
    print("Console Made")
elseif
    msg == "Console off" then
        print("Console Destroyed")
        console:Destroy()
end
end)
点赞
用户9383219
用户9383219

如果您更一致地缩进代码,那么更容易看出语法错误在哪里:

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
        print("Connected")
        if msg == "Console on" then
            console = Instance.new("Folder",workspace)
            console.name = "Console"
            print("Console Made")
        elseif msg == "Console off" then
            print("Console Destroyed")
            console:Destroy()
        end
    end)

更明显的版本:

game.Players.PlayerAdded:Connect(
    function(plr)
        plr.Chatted:Connect(
            function(msg)
                print("Connected")
                if msg == "Console on" then
                    console = Instance.new("Folder",workspace)
                    console.name = "Console"
                    print("Console Made")
                elseif msg == "Console off" then
                    print("Console Destroyed")
                    console:Destroy()
                end
            end)
    end)

您需要在最后添加一个 end) 来关闭 game.Players.PlayerAdded:Connect(function(plr)

game.Players.PlayerAdded:Connect(
    function(plr)
        plr.Chatted:Connect(
            function(msg)
                print("Connected")
                if msg == "Console on" then
                    console = Instance.new("Folder",workspace)
                    console.name = "Console"
                    print("Console Made")
                elseif msg == "Console off" then
                    print("Console Destroyed")
                    console:Destroy()
                end
            end)
    end)
2019-02-28 21:04:51