Skipping Remaining Lines?

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.

Yea maybe spread
if character:FindFirstChild(“Died”) then break end
throughout your script. not the best way though

Is there any way to break the while loop outside it? Neither can I use a even or function to break it sadly.

I suppose you could use a function that accepts a variable on which action to run, and exits out if no players are left.

local function runAction(action)
    if (--[[ player died ]]) then
        return
    end

    if (action == "action1") then
        -- do stuff
    elseif (action == "action2") then
        -- do stuff
    end

    return true
end

local running = true
local actions = { "action1", "action2" }

while running do
    for _, action in next, actions do
        if (not runAction(action)) then
            running = false
            break
        end
    end
end
1 Like

I will try it, it might work :smiley: