【问题标题】:LUA - Simple code supposed to work isn't working (Roblox)LUA - 应该工作的简单代码不起作用(Roblox)
【发布时间】:2020-08-14 04:39:54
【问题描述】:

简单的一段代码,应该可以工作却不工作......我真的无法理解这一点。 这是代码:

local asd = 1
if not asd == nil then
print("works")
end

就是这样,不是打印“作品”,;-;抱歉,如果我遗漏了什么。

【问题讨论】:

    标签: lua roblox


    【解决方案1】:

    运算符优先级骗了你:

    if not asd == nil then
    

    等价于

    if (not asd) == nil then
    

    试试

    if not (asd == nil) then
    

    【讨论】:

      【解决方案2】:

      对不起,回答正在做

      local asd = 1
      if asd ~= nil then
      print("works")
      end
      

      我什至不知道 ~= 是一个令人惊叹的东西

      【讨论】:

      • 你甚至不需要走那么远。您可以使用if asd then print("works") end,因为否则asd 将是nil。分配给asd 的任何值都会自动使其非零。
      • @Josh,我喜欢它的简单性,但明确一点也无妨。虽然 if asd then 在这种情况下有效,但如果 asd 曾经被定义为布尔值,则明显不可靠。
      猜你喜欢
      • 2022-11-14
      • 2018-01-10
      • 2021-07-24
      • 2020-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      相关资源
      最近更新 更多