Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Breaking a loop?

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

✓ Accepted answer

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.

5 comments

0

Can you edit tables from separate scripts?

0

Yes, if the variables are declared. You can call :GetPlayers () from any script anywhere.

0

No I mean can I remove players from the 'alive' table in the round script, from a different script?

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.

0

For future readers: This is a poor solution. It adds completely unnecessary complexity.

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

This discussion is preserved as it was. New answers, comments, and votes are closed.