Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Breaking a loop?

Asked by 10 years ago

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)

2 answers

Log in to vote
0
Answered by
Maxomega3 106
10 years ago

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.~

0
Can you edit tables from separate scripts? systematicaddict 295 — 10y
0
Yes, if the variables are declared. You can call :GetPlayers () from any script anywhere. Maxomega3 106 — 10y
0
No I mean can I remove players from the 'alive' table in the round script, from a different script? systematicaddict 295 — 10y
0
Nevermind, I just used in if function inside the loop, and made a variable called Breakable. It worked perfectly. I'll still accept your answer for trying to help though. systematicaddict 295 — 10y
0
For future readers: This is a poor solution. It adds completely unnecessary complexity. BlueTaslem 18071 — 10y
Ad
Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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 ith 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

Answer this question