Coroutine wait

So I’m back again, sorry haha:

I’m using CompileString to compile a function that will be used clientside, the setup right now is; I have a string which contains all the code that needs to be turned into a function, I create a new string like this:


CodeString = "local "..TIMER_ID.." = coroutine.create(function() "..CodeString.." end) timer.Create(tostring(math.random()), 2, 0, function()  coroutine.resume("..TIMER_ID..")   end)" 

This, in theory should create a coroutine that is resumed every 2 seconds, this means that a player can use coroutine.wait(4) and the script will pause for however many seconds and then continue, my problem is that of course - you can’t have more than one coroutine running at a time, so when I the code:


Console.AddText("TESTING_")
	coroutine.wait(1)
	Console.AddText("						      TESTING_")
 

It is ran perfectly, UNLESS there are two coroutines, then the text that was supposed to be added to console 1, only gets added to console 2, because the coroutine for console 1 is killed off.

I have a feeling I’m just being pretty stupid right now, but I can’t come up with any solutions that provide me with the ability to generate a coroutine out of player code that gives them the ability to use a wait function to pause the script for the specified amount of time and then continue.

Example: Chessnut’s unreleased Gterminal 2 features the wait() function, I’ve spoken to him and he replied telling me it was coroutines, if this was the case, how would I implement the wait function that doesn’t break if I spawn in another console?

As far as I am concerned, coroutines are objects, and therefor can be handled on an infinite instance.



default = coroutine.create(function ()
     local rand = math.random(1,5)
      print("Our random number is ", rand, " !")
      if rand == 1 then
            print("We're going for exceptional!")
            coroutine.yield()
            coroutine.resume(exceptional)
      else
            coroutine.wait(1)
      end
     end)
 
exceptional =  coroutine.create(function ()
      local rand = math.random(1,5)
      print("Our random number is ", rand, " !")
      if rand != 5 then
            print("We're returning to the default!")
            coroutine.yield()
            coroutine.resume(default)
      else
            coroutine.wait(1)
      end
     end)

coroutine.resume(default)


I have no idea if this works, but apparently, this should switch between the ‘default’ and ‘exceptional’ coroutines. When a random number, generated once a second is ‘5’ the ‘exceptional’ coroutine should be run.

idk though, my coroutine is rusty, don’t quote me on this.

Awesome, thanks a lot for this Kiro. Going to give it a go soon, and I will report back ofc.

Turns out what I was using was working, it was my code that was messed up and I found out why: I was using EntIndex, and the setup I have required something unique and of course if I deleted and ent and respawned it; it might have the same ‘EntIndex’, so I implemented a solution where I used EntIndex alongside a math.random().