Lua UI Text == 多个字符串的 or 操作

我真的很菜,需要一点帮助。这个脚本检查玩家界面上的一些文字,如果它等于某个值,比如 game:GetService("Players").LocalPlayer.PlayerGui.Main.Border.ClassLabel.Text == "UNIVERSELORD",那么它就有效。但是如果我添加 or 操作符 game:GetService("Players").LocalPlayer.PlayerGui.Main.Border.ClassLabel.Text == "UNIVERSELORD" or "TANKTOP",它就会毫无例外地执行,而我不知道为什么。如果我只输入一个名字,它就有效,希望得到帮助,谢谢!

原文链接 https://stackoverflow.com/questions/70325568

点赞
stackoverflow用户1442917
stackoverflow用户1442917

你几天前已经因为缺少细节而被关闭了同样的问题。目前不清楚为什么您想要在文本中添加“或”,因此您需要清楚地解释并提供一些上下文示例,以便在某些情况下使用这种用法是有意义的。

就“或”语句而言,它无法产生您想要的结果,因为它返回第一个作为非“false”求值的结果,因此当您有“1或2”表达式时,结果为“1”,因为它从左到右求值,并且这是一个非“false”的结果。类似地,在您的情况下,表达式“UNIVERSELORD”或“TANKTOP”被求值为“UNIVERSELORD”,因为它是遇到的第一个非“false”值。

不幸的是,由于问题中缺少足够的细节,没有人可以告诉您应该将其更改为什么。可能是XY问题

[更新]根据您的评论,您需要将or拆分为两个完整的比较:game:GetService("Players").LocalPlayer.PlayerGui.Main.Border.ClassLabel.Text == "UNIVERSELORD" or game:GetService("Players").LocalPlayer.PlayerGui.Main.Border.ClassLabel.Text == "TANKTOP"

2021-12-12 17:46:33
stackoverflow用户2858170
stackoverflow用户2858170

在 Lua 中,任何非 nilfalse 的值都被认为是 true。

因此在

if (game:GetService("Players").LocalPlayer.PlayerGui.Main.Border.ClassLabel.Text == "UNIVERSELORD" or "TANKTOP") then
  getgenv().Spinloop = false print("Stopped!") return 0;
end

"TANKTOP" 是一个字符串,因此是一个 true 值。

与 true 值或任何值的或操作总是 true。因此,game:GetService("Players").LocalPlayer.PlayerGui.Main.Border.ClassLabel.Text 是否等于 "UNIVERSELORD" 并不重要。结果总是 true。因此,您的 if 条件始终得到满足,无论文本值如何。

2021-12-13 08:02:54