当值为假时立即停止 while 循环。

我有一个 while 循环,当一个值为 false 时,我希望它立即停止。

我猜想如果我只是这样做:

while value == true do
    print("bla")
    wait(2)
    print("blaaaaa")
end

那么它会一直执行 while 循环(包括 wait)直到结束。

这是真的吗?如果是这样,我该如何修复它?

编辑:我的实际代码片段:

function GM:PlayerDisconnected(ply)
    if table.Count(Players) == 1 then
        reset()
    end
end

function GM:PlayerAuthed(ply, steamID, uID)
    if not playing then
      if table.Count(Players) == 2 then
          --开始游戏
          while playing do

          end
      end
    end
end

function reset()
    playing = false

    for k,v in pairs(player.GetAll()) do
        v:Kill()
        v:SetGameModeTeam(2)
    end

    chat.AddText("需要2名玩家!")
end
点赞
用户2597408
用户2597408

你正在寻找的是 break 语句…比如说

if (your condition) then break end

我假设你想在 playing 变量为假时停止循环。在这种情况下,用以下代码替换

while playing do

end
while playing do

    if table.Count(Players) < 2
    then
        break
    end
end

一旦执行 break 语句(也就是仅剩不到两个玩家),while 循环就会退出。你只需在循环中插入一次此 if 语句,最好是放在循环结尾。我无法确定你的实际意图,所以无法肯定。

2013-07-18 22:37:27