Is there a way to run a function that has wait statements and not stop the rest of the code?

read the title

filler text
more filler text

look on task.delay or task.spawn.

example for task.spawn:

print('1')

task.spawn(function()
	wait(5)
	print('3')
end)

print('2')

this script prints “1” and “2” at the same time when the script starts, after 5 seconds it prints “3”.

print('1')

task.delay(5, function()
	print('3') -- the function of 'task.delay', first parameter is duration of the delay. This won't throttle the script
end)

print('2')

a example for task.delay. it basically does the same thing as the code above

1 Like

You can use task.delay!

Example:

function doSomething()
   print(1)

   task.delay(3, function()
      print(2)
   end)

   print(3)
end

doSomething()

The above function will print 1 and 3 without yielding for 2, and 3 seconds later it’ll print 2.

1 Like

Absolutely. You’re now entering the subject of multitasking. Roblox, by default, is cooperatively multitasked, meaning simultaneous code execution is imitated by executing code during downtime in other code. This is implemented through coroutines, for which Luau has a library. Roblox has also introduced their own streamlined cooperative multitasking library called “task”. I will assume you’re looking for simple and immediate “simultaneous” code execution, so the task.spawn function will interest you

1 Like