Quick Question - Return or break?

This is my while loop and I want it to restart if the player count is only 1. Will the break exit the for loop with the 2nd while loop?

while true do
	while true do
		for i=60,0,-1 do
			if #game.Players:GetPlayers() <2 then
				break
			end
		end
	end
end
2 Likes

I recommend you to use break, because it’s made for loops.
So yeah, return exits the function, and break breaks the loop.

Hope it helped!

-. davidxcgdr

1 Like

will break break all loops (while while and for? or will it break just the for loop?

1 Like

Just a quick change for the code:

IsBroken = false
while true do
       if IsBroken then break end
			if #game.Players:GetPlayers() <2 then
                IsBroken = true
                if IsBroken then
				break
            end
	end
end

If the script crashes replace while true do with while true do wait() or while true do game:GetService("RunService").Stepped:Wait().

Break is to get out of loops, return is to exit functions

1 Like

I’d rather do a #game.Players:GetPlayers() check just after the for loop so it breaks instantly if for loop if broken because of 1 player.

1 Like

instead of using a return or break you should do

while Count == 1 do
if you want it to restart while the count is one then once the count is not one itll stop.
unless there is more to i than that

generally you should try and avoid breaks they are an unclean way of leaving a loop.

will it restart if count is 1 again?

no because the count is no longer 1

the loop will keep repeating until “count” is not 1

No, it will simply break.

To be able to restart do

while true do
if Count == 1 then
-- code
end

I’ll go with my method of breaking it in case there is 1 player after for loop is broken.

while true do
	while true do
		for i=60,0,-1 do
			if #game.Players:GetPlayers() <2 then
				break
			end
		end
		if #game.Players:GetPlayers()<2 then break end
	end
end

well what are you trying to achieve with the code and i can give you a better answer?

It’s a fighting game so it should work only when there’s one player. The condition I am preventing is, 2 players before script goes to for loop and in between 1 left. So, the whole code runs if there’s one player after that.

so your trying to get the code to run only when there are two players?

if so you should use a function that is fired when your conditions are met inside the while loop.

more than 1, and Im restarting it if 1 player left in between. for loop has next fight begins in … i seconds thing. Break breaks only 1 nearest loop right?

also is that your actual code you are using because i believe you are trying to do 60 second rounds and if a player leaves then the match resets

that being said i believe his is what you should do

local function ResetMatch()
	-- code that will reset the match 
end

local function MatchBegin()
	for i=60,0,-1 do
			if #game.Players:GetPlayers() <2 then
				ResetMatch()
			end
		wait(1)
	end
end

how can i check if this returned true or false?

local function breakloop()
	if #game.Players:GetPlayers()<2 then
		return true
	end
	return false
end

that would work but it just doesnt seem very efficient.

well the whole match is played in while loop. so i dont wanna reset or something, just break while loop (the inside one) will reset it

look at the code i edited above when you fire the match function for the 60 second match (i think thats what your trying to have " 60 second matches")

it will loop for 60 seconds and if a player leaves then it will fire the Reset Match Function then you should return break possibly if you want to from the “resetmatch” function

1 Like