How to don't wait to function to end?

Hi,

For example i have this:

local function Num2()
	wait(9999)
end
		
local function Num1()
	print("A")
	Num2()
	print("B")
end

So when i do function Num1 it will do function Num2 and i want it to do print(“B”) and don’t wait for Num2 function to end. How can i make it don’t wait for Num2?

1 Like

try this

local function Num1()
	print("A")
	spawn(Num2)
	print("B")
end
1 Like

This situation is perfect for something known as a coroutine, which allows you to run multiple threads at the same times

It’s supposed to be:

spawn(Num2)

Alternatively, you can use this with spawn:

spawn(function()
    Num2()
end)

However, there is a better alternative to the spawn function, as this function has implicit wait – coroutines. Coroutines are similar to spawn:

local function Num1()
	print("A")
    coroutine.wrap(Num2)()
	print("B")
end

or:

local function Num1()
	print("A")
    local coro = coroutine.create(Num2)
    coroutine.resume(Num2)
	print("B")
end

Refer to the Developer Hub Page on Coroutines if you want to know more

you can’t say that a coroutine is a better alternative to spawn when in the end they are the same. spawn is used when time is not critical, as in the case of managing the logic of a game.
coroutines should only be used when parallelism is required and the logic is complex enough. This is not the case.

There were several people talking about why using coroutines over spawn, one of the reasons is because spawn has a hidden wait function which makes it delay.

The aim is to make micro-optimizations, which is bad development practice. You shouldn’t just follow the herd just because.

Not really micro-optimizations at all, there was also another post talking about why you should avoid wait() which is used in the spawn function. (Note, its not talking about things like wait(2) but just wait() )

Here is the link for it

As I said

that article refers to using wait() to manage time and is irrelevant

2 Likes

In truth, two tasks, whether in a different script or “thread”, will not run exactly parallel to each other. Coroutines and spawns basically have no differences other than the fact that the spawn function has an implicit wait, since it has to wait about 30 milliseconds to start since it’s being queued in the task scheduler. Coroutines start instantaneously and you can create them, put them on hold, and start it later as opposed to spawn.

Coroutines generally are the better alternative; spawn even coroutines to work. In this case, it wouldn’t matter which to use since the OP doesn’t seem to be concerned about time

I am aware of the difference between coroutine and spawn because I read the manual. But I do not agree that coroutines are a better alternative because, otherwise, spawn would have already been discontinued.
The real power of a coroutine is not that it executes immediately, but rather the fact that they are pieces of code that can be resumed or suspended, allowing complex problems to be solved more easily