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?
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.
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