Breaking loops in the command bar

So about the command bar, I used the command bar for scripts when I don’t want to press run, but then I wrote a while loop and put code inside it, then I forgot to put a break so it just kept going on forever, and I could not stop it! Is there a way to stop the CommandBar with a while loop after it runs, any help is appreciated, even if you say there is no other way :sad: :happy1:

Never really done this but can’t you just overwrite it by typing another line of code like print("Hello World") or just restart studio

1 Like

You can just set a timeout to the loop, for example 10 seconds.

local timeout = tick()
while tick() - timeout < 10 do
    --your code here
    wait()
end

Or you can create a temporary part somewhere in the game and refrence it, so when you delete the part the loop will error and break

Instance.new("Part", game.Lighting).Name = "somethingwhatever"
while game.Lighting.somethingwhatever do
    --your code here
    wait()
end

I don’t really suggest the second way, but if it works it works :wink:

4 Likes

no, :sad: the command bar can run multiple code at once! But restarting studio MIGHT help :happy2: thanks

Thanks for the tip but I want to know if there is a way to stop it if you accidentally did not do a break inside it, or a timeout or the other code thing… :smiley:

Closing that window is the only way I know of stopping it. The command bar is powerful and I’d avoid using while or repeat loops, or connecting to any of the task scheduler events in there.

If I need a loop in the bar I’ll always use a for loop either over a table or between two numbers.

Unless the loop has frozen your studio by running without any yield, you should be able to save safely and close the window to open it again.

9 Likes

You can also add a bool value somewhere in serverstorage and inside the loop check if the value is equal to true. If it is, then it should break the loop. This makes it so you can check/uncheck the boolvalue instance to stop any loops running from the command bar.

1 Like

You can use global variables

local _G.e = true
while _G.e==true do
print 'e'
wait()
end

then

_G.e=false
1 Like

FYI: global variables in the command bar save, so you don’t have to use _G. Run this:

while not foobar do
    print("yeah")
    task.wait()
end

Then run this:

foobar = true

The loop will stop.

If you want to check what globals you have active in the command bar, use getfenv:

print(getfenv())
5 Likes

I didn’t know getfenv() could get globals, is there any way to “delete” globals to make getfenv() not return them?

1 Like

Set them to nil.

foobar = nil

nil is considered nonexistent and the values will be garbage collected if applicable.

4 Likes

Inserting an appropriate break statement into the while loop somewhere is likely the least complicated solution here.

1 Like