有人能看出这个 Roblox 脚本有什么问题吗?

有人能看出这个 Roblox 脚本有什么问题吗?

local a1 = game.CoreGui.DBXBRGUI.Menu
local a2 = game.CoreGui.DBXBRGUI.Opener
if a1.Visible == true then do
     a2.Visible = false
elseif a2.Visible == true then do
     a1.Visible = false
end
点赞
用户9916865
用户9916865

首先,请不要在“then”之后加上“do”(这就是你的问题)

其次,只需使用else而不是elseif,因为可能有不同的情况。

代码如下所示:

local a1 = game.CoreGui.DBXBRGUI.Menu
local a2 = game.CoreGui.DBXBRGUI.Opener
if a1.Visible == true then
     a2.Visible = false else
     a1.Visible = false
end

我使用常识来说这可能是你想要的:

local a1 = game.CoreGui.DBXBRGUI.Menu
local a2 = game.CoreGui.DBXBRGUI.Opener
if a1.Visible == true then
     a1.Visible = true
     a2.Visible = false else
     a1.Visible = false
     a2.Visible = true
end
2018-06-09 03:46:21
用户5831152
用户5831152

首先,您正在尝试引用 CoreGui,这严格用于 Roblox 的默认 GUI。您制作的任何自定义 GUI 都将在每个玩家的 PlayerGui 中。这是通过执行 game.Players.LocalPlayer.PlayerGui 来引用的。

值得注意的是,对于任何客户端(例如 GUI),在尝试使用对象之前应检查对象是否存在。这可以通过 parent:FindFirstChild(name) 或 parent:WaitForChild(name) 函数来完成。

其次,您在条件语句中使用了“do-end”块。 "do-end" 块需要一个结束符。您还不需要该块,因此建议将其删除。如果我们将 end 添加到代码的正确位置,我们将得到:

if CONDITION then
  -- 执行内容
elseif CONDITION2 then
  -- 执行内容
end

由于缺少结束符,您的代码被解释为:

if CONDITION then
  do
  elseif CONDITION2 then
  do
end

正如您所看到的,"elseif" 出现在第一个 if 的 do 块内,而不是作为第一个 if 的 elseif。您不能没有 if 而使用 "elseif" 语句,这就是出错的原因。

您的代码应该看起来像这样:

local Gui = game.Players.LocalPlayer.PlayerGui:WaitForChild("DBXRGUI")
local a1 = Gui.Opener
local a2 = Gui.Menu
if a1 == true then
   a1.Visible = false
elseif a2.Visible == true then
   a2.Visible = false
end
2018-06-09 06:02:38