我目前正在使用Roblox(使用Lua)進行遊戲。它基本上由幾個迷你遊戲組成。在每輪比賽開始時,比賽中的所有球員被放在一張桌子上並傳送到一個區域。這就是協程進入的地方。由於這輪比賽正在進行中,我想要一個協同程序開始。協程每檢查一次玩家的健康是否低於零,並將它們從currentPlayer表中刪除(如果是)。如何在Lua中結束循環協程?
對不起,如果我沒有正確描述問題,但協程不會屈服。我之前沒有使用過協程,所以我可能試圖錯誤地使用它。我知道你們大多數人不會熟悉Roblox,但Lua的語法是一樣的。
有人能給我一個我將如何結束循環協程的例子嗎?
currentPlayers = {}
roundTime = 60
local lookForWinners = coroutine.create(function()
while coroutine.running do
wait(1)
for i, v in pairs(currentPlayers) do
if v.Character.Humanoid.Health <= 0 then
table.remove(currentPlayers, v)
end
end
end
end)
while wait() do
repeat display("Two or more players need to be in the game.", 1) until #_G.plrs > 1 --Ignore, just checks if two+ players are in game.
display("Picking a map...", 3) pickMap()
teleport(0, 500, 0)
coroutine.resume(lookForWinners)
wait(roundTime)
print("Round over")
coroutine.yield(lookForWinners)
end
感謝您修復代碼,我的錯誤是無法正確發佈。 – user3314993