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

How can I run two functions at the same time?

Asked by 6 years ago
canFire = true

function DoRecoilY()
-- Code for recoil would be placed in here with waits.
end

function Shoot()
--Code for shooting a gun would be placed in here.
end

mouse.Button1Down:connect(function()
    if canFire == true then
    canFire = false
    Shoot()
    wait(FireRate)
    canFire = true
    end
end)

Since DoRecoilY has waits in it it would delay how fast you can shoot. I've heard of something called coroutines but I have no idea how to implement them into this. I've looked at the wiki but I'm a little confused.

0
The recoil could just be an Animation Goulstem 8144 — 6y
0
The recoil just moves the camera, not the arms. Jesse1240 59 — 6y

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
6 years ago
note: running multiple threads is very data intensive, so use coroutines sparingly.

There are multiple ways to run a coroutine. I'll lay them out for you:

  • coroutine.wrap ~ call it using a function, and it will create a new, callable coroutine with the body of that function.
coroutine.wrap(DoRecoilY)()
  • coroutine.create and coroutine.resume ~ call create to create a thread, call resume to run it.
local DoRecoil = coroutine.create(DoRecoilY)
coroutine.resume(DoRecoil)
  • spawn ~ runs a function in a separate thread.
spawn(DoRecoilY)
0
I finally got it. Thanks. Jesse1240 59 — 6y
Ad

Answer this question