我有一个 while 循环,我希望它在值为 false 时立即停止。
我猜如果我这样做:
while value == true do
print("bla")
wait(2)
print("blaaaaa")
end
然后它将继续执行 while 循环,包括(包括等待)直到结束。
这是真的?如果是这样,我该如何解决?
编辑:我的实际代码片段:
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
--Woo hoo start game
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("There needs to be 2 players!")
end
最佳答案
你正在寻找的是一个 break 语句..类似
if (your condition) then break end
我假设您希望循环在 playing 为 false 时停止。在那种情况下替换
while playing do
end
与
while playing do
if table.Count(Players) < 2
then
break
end
end
执行 break 语句的那一刻(即玩家少于两人的那一刻)while 循环将退出。您只需要将此 if 语句放入循环中一次。可能在你的循环结束时。我不能肯定地说,因为我不知道你想做什么。
关于lua - 当值为 false 时立即停止 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17735108/