Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Unable to resume dead coroutine? {Figured out a fix - Solved}

I'm going to simplify my script here:

endroutine = false

func = coroutine.wrap(function()
	while true do
		wait()
		--do some stuff that has to happen with other stuff unless stuff is done
		if endroutine then return end
	end
end)

func2 = coroutine.wrap(function()
	while true do
		wait()
		--do other stuff that has to happen with other other stuff
	end
end)

function dostuff()
	--do stuff
	endroutine = true
	--do more stuff
	endroutine = false
	func()
end

func()
func2()
wait(10)
dostuff()

My issue is that when func() is called in dostuff(), I get an error along the lines of: "Unable to resume dead coroutine."

I tried the following:

endroutine = false

function func()
	while true do
		wait()
		--do some stuff that has to happen with other stuff unless stuff is done
		if endroutine then return end
	end
end

func2 = coroutine.wrap(function()
	while true do
		wait()
		--do other stuff that has to happen with other other stuff
	end
end)

function dostuff()
	--do stuff
	endroutine = true
	--do more stuff
	endroutine = false
	coroutine.wrap(func())
end

coroutine.wrap(func())
func2()
wait(10)
dostuff()

For some reason, the above code makes it so func2() never even runs in the first place, which is a problem since I need func() and func2() to run at the same time.

Simply put: Is there a way I can get both func() and func2() to run at the same time, and after func() has ended via "return end", be able to start it again from it's call in dostuff()?

Preferably without a lot of new lines of code; I like to try and keep my code as compact as possible.

It should be noted that in the actual script, dostuff() is called via a MouseClick event, not directly in the script.

2 comments

0

I just want to point out that you mispelled your variable 'endroutine' at the top of your script.

0

Thank you, fireheart, I've fixed it. It isn't like that in the actual script.

1 answer

You can't call functions that were not scripted prior to them being called. When I say this, I mean the server reads the code from top to bottom. When it comes across func() in the beginning, the server has no idea what you're talking about since it hasn't gotten to that code yet. That's why I suggest always having things CALLED at the bottom of a script, and all functions to run things at the top of a script so there's no chance for anything to error in that fashion! Hope this helped.

2 comments

0

Yes, I would also like to say that it's called a "Run-Time Environment".

0

Ughhh... I should have just posted the actual parts of the script. That's yet another error I made in my "simplification" of it... Sorry, it's not like that in the actual script >.< I'm fixing it now...

This discussion is preserved as it was. New answers, comments, and votes are closed.