Skipping Remaining Lines?

Is there a way to continue a while loop with an event?

I have been wondering for a VERY Long time on how to skip the remaining lines of a while loop when a certain players die, you cannot use break inside an event, even if the event is inside the while loop. I know there is continue in the latest update of lua, but Games like Auto Rap battle was no create recently, and I am very curious on how they did this.

I also cannot create a bunch of if statements every lines I write, because the loop is pretty long and i dont think, writing if statements would make the while loop any better, I know the while loop is probably a bad one, but there is seriously no other way I could have thought off.

I have tried setting bunch of if statements so instead of using waits, I had to a for loop, with if statements and waits of one. I have also tried putting both a function and event, but neither will work because you cannot put break inside a function even though the function is inside the while loop.

while true do
	function breaker()
		break --- will give error here because you cannot put a break inside functions or events
	end
	for i = 1,10 do
		wait(1)
		print("Waited one seconds")
	end
	breaker()
end

I simply want to end the while loop after one player dies, and if the while loop is over 200 lines I dont think its going to be off any help of using any if statements as it will finish the while loop first.

1 Like

Maybe create a variable like “currentlyRunning” and set that to true
Then you would do

while currentlyRunning do

end

Then whenever you’re done, just set it to false and it should stop running

you could make a variable to make it break.
For example, you make a bool variable, if it is true then the loop run, elseif it is false then break the loop

like this

local running = true
while true do
     if not running then break end
     -- code here
end

But I don’t think it would work in this case because the only time it checks the while loop is when it starts, meaning if put it to false, it should only not work after the remaining lines finish, so If the player resets or dies, the game will continue until the loop is finished then it won’t run because i set it to false.

Same thing i said it should’nt work because the loop is 200 lines (I know its a bad practice) but what you are doing will only work at the beginning.

If you set it to false at the end, it will stop the loop since the while loop will check if “currentlyRunning” is true. You could use humanoid.Died to set it to false. I’m not really sure what you’re trying to do

I am trying make a rap battle type game, and if the player leaves or dies the turn of who gets to raps instantly stops and continues what happens after the while loop.

then what is wrong? (30 characters)

local r = true

delay(15, function()
	r = false
end)
while r do
	print("RUNNING")
	wait()
end

print("DONE")

After 15 seconds, r will become false. This will stop printing “RUNNING” and print (“DONE”) since the loop isn’t running anymore if this is what you need.

1 Like

What code about it are you trying to stop it

The while loop is 200 lines with a lot of waits, just by changing a variable will help it.

Not part of the code, if a player dies, they remaining code will stop no matter where it is.

When one player dies it will stop from running

then why don’t u put the remaining code outside of the looop?

because its a round based game?

I don’t understand but maybe you should use a repeat wait until what

Like this

repeat wait() until -- the Char spawns 

Maybe on .PlayerRemoving or .CharacterRemoving or humanoid.Died in another script, add a bool value to their character or player which is named something like “Died” or something. Then throughout the script you could use :FindFirstChild(“Died”), and if it is found, break.

But the loop is over 200 lines.

If I understand correctly…

To my knowledge there’s no way to exit a while loop early when all players die/leave. It’ll finish the while loop before exiting unless you break it, so your best bet is just using if statements before each action.