Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I go about running multiple functions at the same time?

Asked by 9 years ago

I have a CFrame function and would like to trigger it twice with two different objects at the same time.

for the sake of this example, we'll call the CFrame function: Move(part). when I do this:

Move(part1)
Move(part2)

if moves part 1 first, then after part1 is done moving it moved the second one like it should. How would I run them at the same time besides making two scripts?

2 answers

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

Lua code executes line-by-line. There is no way to counter this except by creating new thread, for which you have a few methods:


Method 1: Multiple scripts

The easiest way is to just duplicate the movement script, but this requires you to maintain separate Move functions. There is a better alternative to this, however:


Method 2: Move Module

If you make the Move function a ModuleScript, you can require it in the duplicated movement scripts. This will allow you to call the exact same code from different scripts, solving the main problem of Method 1. This still requires separate scripts, however. If that's not a possibility, then...


Method 3: Coroutines

Coroutines (pronounced co-routines) are threads of executable Lua code. They have to be resume'd to be run, which can be done instantly. Unlike Methods 1 and 2, Coroutines are created and run from the same Script.

Assuming Move takes the same amount of time for both calls, you can write the code like this:

coroutine.resume(coroutine.create(Move), part1) --Move is a reference to the Move function. It is *not* called here. This code will call the function inside its own thread.
Move(part2)

If they don't, you can make both calls run in a coroutine and yield the main thread (the script itself) until some value is changed to indicate completion.

0
As an aside to this, the reason you'll see functions with two sets of parentheses: loadstring()() is that the First function call *returns* a Lua function, which is then immediately called itself. adark 5487 — 9y
0
Wow, thats a lot to take in, I'll look up coroutines. BTW, since you said (Move), part1 is not called there, how would you call it using a Coroutine? Orlando777 315 — 9y
0
Exactly as I said there. I'll rewrite that sentence to be less confusing. Basically, you're calling `resume` and `create` functions of the coroutine library, but you're *not* calling Move itself. adark 5487 — 9y
Ad
Log in to vote
-1
Answered by 9 years ago

make two functions that trigger at the same time by the same thing, all in one script -- edit-- heres a script with two function working at the same time.

function kill(hit)
    human = hit.Parent:findFirstChild("Humanoid")
    if human ~= nil then
        human.Health = human.Health - 5
    end
end

function disco(hit)
    if human ~= nil then
        script.Parent.BrickColor = BrickColor.Random()
    end
end
script.Parent.Touched:connect(kill)
script.Parent.Touched:connect(disco)

this is a simple one that works perfectly --edit--

this runs them both at the same time, there is no wait in-between them

0
That doesn't work. You cant trigger two at the same time as explained above. Orlando777 315 — 9y
0
yes you can its quite simple, make the functions, and use the same trigger to activate them, i do it all the time chimmihc -2 — 9y
0
simple chimmihc -2 — 9y

Answer this question