I have a table, basically the alive players: (This is a round script)
alive = {}
And something that checks if a player is alive, and adds them to the table.
for i,v in pairs(game.Players:GetPlayers()) do if v:FindFirstChild("InGame") then v.InGame.Value = true table.insert(alive, v.Name) end end
And then somewhere in my round script, I'm trying to end the round within 100 seconds but I want it to only end if 100 seconds have passed, or if only 1 player, or no players are alive. So this:
for time = 1,100 do wait(1) for i,v in pairs(game.Players:GetPlayers()) do if v.InGame.Value == false then table.remove(alive, i) end if #alive <=1 then break elseif #alive == 0 then break end end end
The problem is that loop never ends, how can I make it end if only 1 player's BoolValue is true, or nobodys' is true?
(I have a separate script that turns it to false when someone dies)
Well, I was looking into how far a break goes when you call it, and this is what I found:
while wait (.5) do print 'hel' for i = 1,10 do print 'lo' if i == 2 then print 'world!' break end end end
When the break triggers, it stops the for loop and the if statement, but not the while loop. This means in your script, it would break your in pairs for loop, but not your for 1,100 loop.
My suggestion is to have a seperate script to set the values to false when someone dies, so you don't need the extra for loop.
~Just remember that if someone answers your question you and that person will get reputation if you accept their answer. There should be a button for it near the voting on the answer.~
There are several problems that I see with the code you have posted.
The problem you are having is partly symptomatic of this.
You make an assumption in this design which is a very bad one.
You assume that the index i
in the list of players is equivalent to the index i
in the list of alive
.
This is broken by 1) players joining 2) players leaving. This makes your solution fundamentally wrong, unfortunately.
In addition, you perform table.remove
at the position i
to correspond to the i
th player. However, table.remove
moves all of the elements after it down one, so after the first player is removed, this correspondence is completely destroyed.
After that is fixed, however, the solution to your break problem is simple. The break statements should be done after processing all of the players, rather than in the middle of processing them:
for time = 1,100 do wait(1) for i,v in pairs(game.Players:GetPlayers()) do -- {Incorrect logic} end if #alive <=1 then break elseif #alive == 0 then break end end