How to stop code on command!?

local function StartGame()
     -- this can have any amount of code that yields including loops
end


local function End()
     -- when end is called I want it to stop running the game code
end

If the StartGame function has connections in it, that can be stopped easily by just putting them in a maid and then destroying.

But what about any arbitrary code that could be running? I could make a boolean flag, but then I would have to check the flag after every time the code yields (which is does often because I have a lot of task.wait()'s and loop timers)

local IsRunning = false

local function StartGame()
     IsRunning = true

     -- code
 
     task.wait(3)
     if not IsRunning then return end -- need to check because "End" could have been called while this code was yielding

     -- more code

     while IsRunning do -- need to check every single iteration in case End was called
          -- code
          task.wait()
     end
end

local function End()
    IsRunning = false
end

As you can see this gets tedious really quickly, but more importantly it doesn’t ensure that all the code really stopped. If I forget to put a check I can have rogue function continue to run. Then what happens if I call StartGame again and both instances of the function are changing a global variable screwing up the game and making it really difficult to track down the bug.

Essentially I want to know how I can write my code where I can effectively have a control panel that can start and stop any game mechanic at will.

2 Likes

Well, you could use Events from a module like sleitnicks Signal, which would allow you to connect to and End event, which would then eventually be allowed to be disconnected. Now, whether or not this will instantly halt execution of Start is unbeknownst to me. You could also use multiple events to provide feedback between the functions, one to call for a stop and the other as a response, similar to a promise or just a return.

1 Like

I could do that with a Maid

Not really sure what you mean.

I want to be able to stop any piece of code on a dime without having to fill the entire thing with if running then statements.

For example I have a lot yielding code because I want time to elapse before something happens, in the beginning I can do if running then, but then everytime I have a task.wait() I would have to do it again. I would also have to put it in every for loop and in every function that yields my code might call.

There has to be a concrete way to stop a programs because millions of programs can just stop with the press of a button.

Maybe I am doing something wrong, maybe I am not supposed to yield code but then how am I supposed to have timers and things that need to wait.

1 Like

do you mean that you want to make a the function start not stop the upon lines, right? You’ll need to study about spawn and coroutines, these functions from lua are able to create a new thread.

1 Like

Please elaborate and show code so I understand

local function StartGame()
spawn(function()
     IsRunning = true

     -- code
 
     task.wait(3)
     if not IsRunning then return end -- need to check because "End" could have been called while this code was yielding

     -- more code

     while IsRunning do -- need to check every single iteration in case End was called
          -- code
          task.wait()
     end
 end)
end

If you want to know more about, read this What is spawn(function) end) used for?

okay but you didn’t help me with my question.

I’m aware of what spawn and coroutine do, can you explain how I can use it to stop a function from continuing?

This could probably be achieved through Coroutines.

Maybe you don’t understand how functions works, when you execute a function it’ll read all the lines, when you make while, the function won’t end until while loop ends,

local isrunning = false

local function start()
      print("Starting")
      isrunning = true
      
      while isrunning do
            print("Executing while loop") -- Debbuging
            task.wait()
      end
end

start() -- Executing function

print("Start function finished") -- You need to know that it won't print, because the function is not finished yet, because while loop is infinity executing, so we'll need to create a new thread, using spawn().

You are not telling me information relevant to my question. Please reread my question before replying.


@wf_sh do you know how? I want to be able to end any program with an event. Like if I press the “x” on the browser, chrome shuts down. What if while I was pressing X another thread was doing

task.wait(2)
-- display some stuff that should only be displayed when chrome is open.

Maybe my code isn’t supposed to yield or something but what if I need to wait.

There has to be a methodical way to write a program so that you can end it easily with a single event. Millions of applications have a close button.

1 Like

Oh i’m sorry i don’t understand about task library.

Coroutines give you full control of a thread, consider using them for this situation.

Wrap the entire script in a separate script connection, that relies on a shared variable to stay connected. (Like a value in workspace) And when the value disables, the script connection breaks and the entire thing stops.

Okay, can you show me an example on how I would do that. I’m confused on how that works.

could you give anymore context at all? if you truly want just an x-button for some code you do know that enabling the Disabled property to scripts instantly stops them right?