generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Coroutine Help

Previous Thread :: Next Thread 
blooo127127 is not online. blooo127127
Joined: 22 Jul 2010
Total Posts: 403
18 Aug 2014 05:44 PM
In a previous thread several people pointed out that I can use coroutine to fire a function independently from another function. However, I needed to that independently fired function more than once, and it seems that coroutine.create and coroutine.resume only allow that function to be used once. I'm not trying to use event handlers to start functions. Example:

local doSomething = coroutine.wrap(function(num)
wait(10)
print("doSomething Test" .. num)
end)

function main()
print("main Test1")
for i = 1, 5 do
doSomething(i)
end
print("main Test2")
end

main()

It will only fire doSomething once before giving me an error about it being a dead function or not a function at all. It works one time before this, and I have to reset PlaySolo in order to restart the script.

I'm trying to output:

main Test1
main Test2
doSomething Test1 -- Each of these should appear instantly around 10 seconds
doSomething Test2 -- later
doSomething Test3
doSomething Test4
doSomething Test5

Instead of:

mainTest1
mainTest2
Example.Example:8: cannot resume dead coroutine
Report Abuse
L0cky2013 is not online. L0cky2013
Joined: 30 Jul 2012
Total Posts: 1446
18 Aug 2014 05:50 PM
Me, I would use spawn in this case.


function doSomething(number)
Spawn(function()
wait(10)
print("doSomething Test" .. number)
end)
end
Report Abuse
blooo127127 is not online. blooo127127
Joined: 22 Jul 2010
Total Posts: 403
18 Aug 2014 06:26 PM
I am using Spawn but am not getting the results I need. I need to run a function without it interfering with the current function's timing.

local bleh = (function()
wait(5)
print("bleh Test1")
end

function main()
print("main Test1")
Spawn(Bleh())
print("main Test2")
end

main()

Would give me an output of:

main Test1
bleh Test1 -- After waiting for 5 seconds
main Test2

because main() runs the code from bleh() before continuing. I don't want that to happen.
Report Abuse
Prehistoricman is not online. Prehistoricman
Joined: 20 Sep 2008
Total Posts: 12490
18 Aug 2014 06:47 PM
Spawn(Bleh())

will call Bleh and pass what it returns to the Spawn function. You do not want this. Fixed code:

local bleh = function()
wait(5)
print("bleh Test1")
end

function main()
print("main Test1")
Spawn(bleh)
print("main Test2")
end

main()
Report Abuse
blooo127127 is not online. blooo127127
Joined: 22 Jul 2010
Total Posts: 403
18 Aug 2014 08:49 PM
Thank you very much, this has answered my question. Up until this point I didn't even know that coroutine and Spawn were things, reflects on my knowledge of Lua :/
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image