Question, How Would I Stop This Loop?

Hi as the title says, How do I stop this loop? So heres the loop:

while true do
    --I know what I'm going to do inside this...
end

Do I Add something like:

Local loop = true --This doesn't work, but Something else could...

All Answers help, Thank you.

3 Likes

Using the word break on where you would want to stop the loop should work.

2 Likes

Use the break keyword to cancel a loop entirely.

1 Like

how do you use Break? Like loop.break?

Just break by itself.

Example:

while true do
    break --cancels loop entirely
end
2 Likes
for index = 1, 10, 1 do
    if index == 5 then
        break
    end
end

Or adding to the post above, for loops also work with break

3 Likes

Would I be able to put break after:

while true do
    --nothing here
end
if "something" then
    break
end

would that work?

No. You will need to nest it inside the break, or else you will get an error:

while true do
    if "something" then
        break
    end
end

No you can’t do that.
You need to nest it in the loop for it to work or else the program has no idea what you’re talking about
However, if you nested it correctly it would work.
Breaks work in practically any instance.

You can learn more about loops on the api-refernece here :slight_smile:
https://create.roblox.com/docs/tutorials/scripting/basic-scripting/intro-to-scripting#looping-code

Would I be able to add a button function in while true do like this?

local button = script.parent
while true do
    local function onButtonActivated()

    end
end
button.Activated:Connect(onButtonActivated)

It works, but that’s a bad idea.
It will constantly set a function, meaning that you will experience issues (going off my JavaScript knowledge).

Also, you won’t be able to call the function afterward without a break, since Lua(u) yields functions and loops.

Or maybe i can try:

local button = script.parent
while true do
    if "Something" then
        local function onButtonActivated()

        end
    end
end
button.Activated:Connect(onButtonActivated)

"Something" will always evaluate true, since it is an existing variable.

Also, you still didn’t use a break, meaning that the loop will still yield.
PLUS, your function is also declared out of scope.

Personally, i’d fix it like this:

local button = script.parent
local val = "Something"
while true do
    if val then
        local function onButtonActivated()

        end
        button.Activated:Connect(onButtonActivated)
        break
    end
end
1 Like

I see, Thank you so much @daisytheghostchild98

1 Like

You’re welcome! :slight_smile:

3 0 chars limit grrrrrrrrrrrrrrrrrrrrrrrrrr

1 Like
local runing = true

someSignal:Connect(function() -- the signal will stop the loop
    runing = false
end)

while runing do
     -- the code will run while runing == ture
end

Here is how I would do it:

local button = script.parent
local val = "Something"
while true do
    if val then
        break
    end
end
local function onButtonActivated()
    if val then
        -- Do something
    end
end
button.Activated:Connect(onButtonActivated)

Also, you need to add some yield / wait using task.wait() to your loop outside of the if statement, or else your script will crash.

That alternative makes no sense. I defined the button event and stopped the loop.

Why would I need another if statement in the event afterward?

EDIT:
Well, it actually does make sense with Lua(u)'s yield logic, but I find the if unnessecary.

If val is true, then we break out of the loop.

And if val is true, then we run the code inside of the function that is connected to the event whenever the event fires. It does the same thing. But from my own experience it’s generally not good practice to initiate events / functions inside of loops.

But then we just added an unnecessary if statement. What if val becomes something that evaluates false?