我目前正在使用 Roblox(使用 Lua)开发游戏。它基本上是由几个小游戏组成的。在每一轮开始时,游戏中的所有玩家都被放在一张桌子上并传送到一个区域。这就是协程发挥作用的地方。随着回合的进行,我想要一个协程开始。该协程每秒检查玩家的生命值是否低于零,如果是,则将其从 currentPlayer 表中删除。
抱歉,如果我没有正确描述问题,但协程不会产生。我以前没有使用过协程,所以我可能试图以错误的方式产生它。我知道你们中的大多数人不会熟悉 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