Is there a way to restart a function after you paused it?

Hey! So basically what I am trying to achieve here is when some player leaves the game then the function will be paused by using the coroutine.yield() function, however when I do this I want to be able to stop the function (which I just did) but also restart the function after me stopping it. So is there any way to achieve this? I’ve been researching everywhere about coroutines and there doesn’t seem to be a function that can restart a function, if I try using a boolean value to check whether its true or not the script will just continue where it was paused at.

Here is an example of a coroutine I made:

local Thread = coroutine.create(function()

  -- game code

game.Players.PlayerRemoving:Connect(function(player)
if player.Name == mainPlayer then
        coroutine.yield("threadPaused")
             -- How do I restart the function again so that the game will restart when the main player dies?
else
             end
      end)
end)

coroutine.resume(Thread)


Hmm, I don’t really know, maybe you can add a super long wait(100) or something, and write code that you can stop/pause. Or you can do a loop and break.

Tamzy, I recommend checking this great guide by @ReturnedTrue about Coroutines! Coroutines - When and how to use them
You should find your answer there.

1 Like

Hey! I am just giving a hand possibly if I can help? No need for harm, man!

You need to resume it again after you yielded it. Do some kind of variable that will detect when it will yield or not

If I resume it after I yielded it… the function will continue from where it paused not restart, I want it to restart.

Also, I 100% know how to use coroutines, what I’m trying to say is that I cannot find a way to restart a function after it has been paused using coroutine.yield()

So there are no reasons to use coroutine there, you can just return the function instead of yielding and then running the function again.

4 Likes

Ahh, right! So if I execute the function and then return it then it will make the function restart and stop the thread, right?

For example:


playerLeft = false

local function Thread()

  -- game code

	print("Hi")
	
	-- player left
playerLeft = true
	print("bye")
	
	if playerLeft == true then
		Thread()
		return
	end
	print("mhm")
end

Thread()

I haven’t been used to using return much but if this was a loop I would do break but it isn’t so returning is the option.

No, return stops the current thread and returns a value to the function. Example:

local function myReturnTest()
      print("Hello!")
      return "Hey"
      -- The function ends here.
end
myReturnTest()
-- Prints Hello!
wait(2)
print(myReturnTest()) -- Prints Hello! and prints Hey!

Note that you can return multiple values at once by doing something like this:

local function myThreeLines()
      print("Wow")
      return "Hi", "Hello", "Hey"
end
myThreeLines()
-- Prints Wow
wait(2)
local FirstLine, SecondLine, ThirdLine = MyThreeLines()
print("First: "..FirstLine.. "Second: "..SecondLine.. "Third: "..ThirdLine)

--Prints First: Hi Second: Hello Third: Hey

print(myThreeLines) -- Prints Hi Hello Hey

Yeah thats what I said, in my case I execute the function and use return to stop the thread from running.

Yes thats how you use it, either to stop a function or to stop it and give a value to it.

1 Like

Just for some insight on coroutines:

game.Players.PlayerRemoving:Connect(function(player)
    if player.Name == mainPlayer then
        coroutine.yield("threadPaused")
    end
end)

You have an RBXScriptSignal here. It runs whenever a player is removed and is completely independent from the function its wrapped in. This also means its a different coroutine thread then ‘Thread’, so yielding here only yields the code handling when a player leaves (not the coroutine thread it was connected in).

1 Like

I understand, so to make sure it yields the actual thread it would be better if I added a boolean variable to check if its true then yield it outside the scope, like this:

local ShouldBeYielded = false

local Thread = coroutine.create(function()

  -- game code

game.Players.PlayerRemoving:Connect(function(player)
if player.Name == mainPlayer then
       ShouldbeYielded = true
             -- How do I restart the function again so that the game will restart when the main player dies?
else
             end
      end)

if ShouldBeYielded == true then
coroutine.yield("threadPaused")
    end
end)

coroutine.resume(Thread)
1 Like

I think you should back up. What exactly are you trying to achieve with this?

There are ways to yield and resume functions/coroutines, but its almost certainly not the best way to tackle your problem.

See this also: http://xyproblem.info/

Yeah, thats using a variable visible by both threads. So that will yield the ‘actual’ thread if you make that check after the event fires.
But in a way you’ve kind just complicated:

function main()
    -- Do stuff
    if not (game.Players:FindFirstChild(mainPlayer)) -- Main player quit
        coroutine.yield()
    end
end

plus in this scenario, I think you were on a good track with returns, because then you’re able to restart the actual function. Yielding just pauses the function until you resume it, which isn’t what you want.

1 Like

Are you talking about keeping a coroutine in a paused state?

local players = game:GetService("Players")

local function func()
	print("foo")
	coroutine.yield()
	print("bar")
end

local function onPlayerAdded()
	local thread = coroutine.create(func) 
	coroutine.resume(thread) -- 'foo'
    wait(1)
    coroutine.resume(thread) --> 'bar' prints when you resume, the thread with the function 'func' basically resumes from where it was put in a paused state
end


players.PlayerAdded:Connect(onPlayerAdded)

Nope, I am talking about how to restart a function after it has been yielded, just like when you have yielded the coroutine I want the function to be restarted after it has been yielded but I actually didn’t need to use any coroutines, I instead just executed the function and returned the thread. But it is fine because @AstralBlu_e got the solution.