Roblox Lua if 语句即使在值不正确时仍在执行
Roblox Lua if statement still executing even when the value isn't true
这是我游戏中的一个主要问题。我一直在尝试制作一个脚本来检测值何时为 1、2、3、4 或 5。但是,即使值为 0,if 语句仍将执行,使计时器在任何人进入之前启动电梯。这真的很烦人,我无法修复它。这是脚本:
local players = workspace.TestMode.Players
players.Value = 0
wait(5)
script.Parent.Text = "Waiting for players..."
function StartTimer()
while true do
if players.Value == 1 or 2 or 3 or 4 or 5 then
script.Parent.Text = "15"
print("enough players")
wait(0.1)
else
script.Parent.Text = "Waiting for players..."
print("not enough players")
end
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "14"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "13"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "12"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "11"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "10"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "9"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "8"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "7"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "6"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "5"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "4"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "3"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "2"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "1"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "Teleporting players..."
else
script.Parent.Text = "Waiting for players..."
end
end
end
StartTimer()
在 Lua 中,除 nil
和 false
之外的所有值都是 truthy
,因此当在布尔上下文中评估时,它们将被视为 true
。
if players.Value == 1 or 2 or 3 or 4 or 5 then
这意味着你的if条件是players.Value == 1
true
或者是2
true
等等。 2
总是 true
。所以你的 if 条件总是 true
.
您的情况应如下所示:
if players.Value == 1 or players.Value == 2 or players.Value == 3 or players.Value == 4 or players.Value == 5 then
或者你可以有一个更简单的条件,这里有一些想法:
if players.Value > 0 and players.Value <= 5 then
conditions = {[1] = true, [2] = true, [3] = true, [4] = true, [5] = true}
if conditions[players.Value] then
这不是答案,因为上面的人回答了,但这只是为了告诉您脚本有什么问题,以便您从中吸取教训。你做了 if players.Value == 1 or 2 or 3 or 4 or 5。当你使用 if 语句然后你在它里面有一个 or 语句。您必须完全重新启动代码。例如,如果 players.Value == 1 或 players.Value == 2 或 players.Value == 3 等,它就会起作用。这就是它不起作用的原因。
这是我游戏中的一个主要问题。我一直在尝试制作一个脚本来检测值何时为 1、2、3、4 或 5。但是,即使值为 0,if 语句仍将执行,使计时器在任何人进入之前启动电梯。这真的很烦人,我无法修复它。这是脚本:
local players = workspace.TestMode.Players
players.Value = 0
wait(5)
script.Parent.Text = "Waiting for players..."
function StartTimer()
while true do
if players.Value == 1 or 2 or 3 or 4 or 5 then
script.Parent.Text = "15"
print("enough players")
wait(0.1)
else
script.Parent.Text = "Waiting for players..."
print("not enough players")
end
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "14"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "13"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "12"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "11"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "10"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "9"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "8"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "7"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "6"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "5"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "4"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "3"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "2"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "1"
else
script.Parent.Text = "Waiting for players..."
end
wait(0.1)
if players.Value == 1 or 2 or 3 or 4 or 5 then
wait(0.9)
script.Parent.Text = "Teleporting players..."
else
script.Parent.Text = "Waiting for players..."
end
end
end
StartTimer()
在 Lua 中,除 nil
和 false
之外的所有值都是 truthy
,因此当在布尔上下文中评估时,它们将被视为 true
。
if players.Value == 1 or 2 or 3 or 4 or 5 then
这意味着你的if条件是players.Value == 1
true
或者是2
true
等等。 2
总是 true
。所以你的 if 条件总是 true
.
您的情况应如下所示:
if players.Value == 1 or players.Value == 2 or players.Value == 3 or players.Value == 4 or players.Value == 5 then
或者你可以有一个更简单的条件,这里有一些想法:
if players.Value > 0 and players.Value <= 5 then
conditions = {[1] = true, [2] = true, [3] = true, [4] = true, [5] = true}
if conditions[players.Value] then
这不是答案,因为上面的人回答了,但这只是为了告诉您脚本有什么问题,以便您从中吸取教训。你做了 if players.Value == 1 or 2 or 3 or 4 or 5。当你使用 if 语句然后你在它里面有一个 or 语句。您必须完全重新启动代码。例如,如果 players.Value == 1 或 players.Value == 2 或 players.Value == 3 等,它就会起作用。这就是它不起作用的原因。