How do you make a function that doesn't stop the script when it plays?

I want to make a function that doesn’t interrupt the script.

local function Example()
  wait(3)
print("A")
end

wait(2)
Example()
wait(3)
print("B")

I want the script to print A and B at the same time. But instead, it would print A, then print B 3 seconds later.

What I want to happen:
waits 5 seconds, prints both A and B

What happens:
waits 5 seconds, prints A, waits 3 seconds, prints B

How would I do this with functions?

1 Like

use coroutine so it wont yield

local function Example()
	task.wait(3)
	print("A")
end

coroutine.wrap(Example)()

task.wait(3)
print("B")
5 Likes

Try this:

local function Example()
	task.wait(3)
	print("A")
end

task.wait(5)

task.spawn(Example)
print("B")
3 Likes

You have 2 options here.

  1. Using a thread such as task.spawn that @Katrist mentioned (do not use coroutine here like @BirdieI90 mentioned)
  2. Using task.delay like the following example.
task.delay(3, function() 
print("A")
end)
task.wait(3)
print("B")

Hope it helps.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.