【问题标题】:Why isn't this code changing the variable to True Or False为什么这段代码不将变量更改为 True 或 False
【发布时间】:2019-10-25 08:58:58
【问题描述】:

问题

所以目前,我正在 Roblox 中制作游戏。我正在对我的一个 GUI 进行补间,但代码并没有改变我正在使用的名为 state 的变量。 state var 应该告诉它是打开还是关闭(如果打开,State 将 = true,否则,State 将 = false)。

我尝试将变量设为本地变量。但仍然是相同的输出。我通过打印state var 检查了输出。这将始终与默认值相同。

代码


-- Local Variables
local Frame = script.Parent.Parent.Parent.Parent.Parent.MinerGuiManager.MinerFrame
State = false
local Button = script.Parent.Button


-- Open/Close Statements

if State == true then
    Button.Text = 'Close!'
    script.Parent.Button.MouseButton1Click:connect(function()
    Frame:TweenPosition(UDim2.new(0.3,0,1.2,0))
    State = false
end)
end

if State == false then
    Button.Text = 'Open!'
    script.Parent.Button.MouseButton1Click:connect(function()
    Frame:TweenPosition(UDim2.new(0.305,0,0.25,0,'Bounce',1.5))
    State = true
end)
end

我希望代码的输出将 var state 在打开时设置为 True,在关闭时设置为 False。

【问题讨论】:

  • Lua 变量区分大小写,在您的编辑中,您输入 state 而不是 State - 这并不能解决您的问题。

标签: lua roblox


【解决方案1】:

您需要小心连接 Mouse1Click 事件侦听器的方式。

当您从上到下阅读脚本时,您会看到由于State 开始时为假,您连接的唯一侦听器是第二个侦听器。这意味着当您单击按钮时,它只会将 Frame 补间到 Open 状态。最好编写一个单击处理程序来处理每次单击时的此逻辑。

local Button = script.Parent.Button
local Frame = script.Parent.Parent.Parent.Parent.Parent.MinerGuiManager.MinerFrame
State = false

Button.MouseButton1Click:connect(function()
    if State == true then
        Button.Text = 'Close!'
        Frame:TweenPosition(UDim2.new(0.3,0,1.2,0))
    else
        Button.Text = 'Open!'
        Frame:TweenPosition(UDim2.new(0.305,0,0.25,0,'Bounce',1.5))
    end)

   State = not State
end)

【讨论】:

  • 当我把它放入 Roblox 时,Roblox 输出一个错误,上面写着 ``` Expected Identifier got')' ``` 这会出现在end)
  • 抱歉打错了。我忘了关闭连接功能。会更新答案
【解决方案2】:

您在Frame:TweenPosition(UDim2.new(0.3,0,1.2,0)) 行之后缺少State = false。在将其更改为 true 后,您永远不会将其值切换回 false

【讨论】:

  • 您添加了一个不同的变量 (state),而不是您在其他地方使用的变量 (State)。
猜你喜欢
  • 1970-01-01
  • 2019-10-30
  • 2019-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多