Can You run 2 functions at at a time? If so, how do do so?
Yes, but, most of the time this will be not the best solution to a problem.
As a simple example, two Script objects will run at the same time. Each Script object is given its own "thread", or, as Lua calls it, coroutine (routine being a piece of a program and co indicating that multiple are working together, as in coworker)
Coroutines are not true threads. Only one coroutine can execute at a time; ROBLOX just quickly switches between them. Many coroutines at once move very slowly, which is just one reason to avoid using them too much.
Here is the PIL description of coroutines:
A coroutine is similar to a thread (in the sense of multithreading): a line of execution, with its own stack, its own local variables, and its own instruction pointer; but sharing global variables and mostly anything else with other coroutines.
Lua uses the coroutine
library to create new coroutines. Coroutines Wiki Article
routine = coroutine.create( func ) coroutine.resume( routine )
coroutine.create
makes a new coroutine, given a function.
coroutine.resume
"unpauses" (resumes "playback") of a coroutine object / starts a coroutine.
There are other controls like coroutine.yield
that let coroutines produce results.
If you're repeatedly resume
ing the same function, you can instead use coroutine.wrap
.
playRoutine = coroutine.wrap( func ) playRoutine()
coroutine.wrap
returns a function which will resume your coroutine each time it's called (which will usually just be once)
ROBLOX provides two new functions to do this in a little less code.
spawn(func)
will create and run a coroutine with a given function. Spawn
delay(func, time)
will, after time
seconds, create and run a coroutine with the given function. Delay
Here are some examples using spawn
:
function blues() while true do game.Workspace.A.BrickColor = BrickColor.new("Bright blue"); wait(1); game.Workspace.A.BrickColor = BrickColor.new("Black"); wait(1); end end function reds() while true do game.Workspace.B.BrickColor = BrickColor.new("Bright red"); wait(1); game.Workspace.B.BrickColor = BrickColor.new("Black"); wait(1); end end spawn(blues) spawn(reds) -- OR spawn(blues) reds()
I mentioned at the beginning that usually coroutines aren't the best solution to a problem. For example, with the above example, it makes much more sense to combine the two into one function:
function redsAndBlues() while true do game.Workspace.A.BrickColor = BrickColor.new("Bright blue"); game.Workspace.B.BrickColor = BrickColor.new("Bright red"); wait(1); game.Workspace.A.BrickColor = BrickColor.new("Black"); game.Workspace.B.BrickColor = BrickColor.new("Black"); wait(1); end end redsAndBlues();
See Also: https://scriptinghelpers.org/questions/11836/can-one-script-run-many-items for more complicated comparison of with coroutine and without
Yes, you can.
If you need more help, don't hesitate to contact me! :)
Here is an example:
function printone() -- Creates printone() print "printone" end function printtwo() -- Creates printtwo() print "printtwo" end while true do wait(1) -- Every second it will run both functions using "spawn". spawn(printone) -- You could just use printone() because there is no loop or anything. spawn(printtwo)-- You could just use printtwo() because there is no loop or anything. end
You can also do this:
function one() print "one" -- When this function is run it will print one but will also do the second function. spawn(two) -- You could just use two() because there is no loop or anything. end function two() print "two" end