Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I run two functions at the same time?

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.

2 comments

0

The recoil could just be an Animation

0

The recoil just moves the camera, not the arms.

1 answer

✓ Accepted answer

######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)

1 comment

0

I finally got it. Thanks.

This discussion is preserved as it was. New answers, comments, and votes are closed.