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 » Scripters
Home Search
 

Re: Coroutines

Previous Thread :: Next Thread 
inception101 is not online. inception101
Joined: 19 Dec 2010
Total Posts: 738
19 Mar 2016 11:59 AM
I'm trying to make a script that has a while loop with conditionals that run certain coroutines depending on (in this case) a random number. My problem is when I run the same coroutine twice (which I intend to do), it runs nothing because the coroutine is "dead". How would I make the coroutine start over after it is done running?

Coro = Coroutine.create(function()
Debou = false
wait(1)
print("A")
Debou = true
end)

while wait(1) do
if Debou then
numb = math.random(1,3)
if numb == 1 then
coroutine.resume(Coro)
elseif numb == 2 then
print("B")
elseif numb == 3 then
print("C")
end
end
end

This would only run the coroutine once and skip over the "beef" of the function every time it has been called after the first.



inception101 trade, development, design
Report Abuse
62GB is not online. 62GB
Joined: 03 Oct 2011
Total Posts: 4157
19 Mar 2016 12:13 PM
Coro = function()
Debou = false
wait(1)
print("A")
Debou = true
end

while wait(1) do
if Debou then
numb = math.random(1,3)
if numb == 1 then
coroutine.resume(coroutine.create(Coro()) --fix these parentheses, im sure i did them wrong
elseif numb == 2 then
print("B")
elseif numb == 3 then
print("C")
end
end
end
Report Abuse
inception101 is not online. inception101
Joined: 19 Dec 2010
Total Posts: 738
19 Mar 2016 02:29 PM
Wouldn't that just be the same thing as allocation a function then?


inception101 trade, development, design
Report Abuse
62GB is not online. 62GB
Joined: 03 Oct 2011
Total Posts: 4157
19 Mar 2016 02:30 PM
What I always do is this

coroutine.resume(coroutine.create(function()
--blah
end))


Without specifying a pre-made coroutine function, I just run this when ever I need to run a separate thread. But anyways, it doesn't like you need to be using coroutines for this script.
Report Abuse
inception101 is not online. inception101
Joined: 19 Dec 2010
Total Posts: 738
19 Mar 2016 02:36 PM
I would rather not use coroutines, but I want the while loop to run in sync with all the other codes that are dependable in the while loop. But I think I understand, now to try it out! :) Thanks!


inception101 trade, development, design
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
19 Mar 2016 02:36 PM
Okay, here's the really annoying explanation. Fasten your seatbelts.

DON'T US3 COROUT1N3S. P3OPL3 OFT3N DON'T UND3RST4ND HOW COROUT1N3S WORK 4ND WH3N TO US3 TH3M, 4ND G3T TH3MS3LV3S 1N 1SSU3S L1K3 TH1S.

US3 SP1WN 1NST34D, B3C4US3 1T R34CH3S TH3 S4M3 3FF3CT YOU W4NT3D W1TH L3SS COMPL1C4T1ONS 4ND R4C3 COND1T1ONS.
YOU'R3 W3LCOM3.
Report Abuse
inception101 is not online. inception101
Joined: 19 Dec 2010
Total Posts: 738
19 Mar 2016 02:40 PM
eLunate, sorry if I'm annoying you, but I can't read your (special) language you type in. What do you mean by "SP1WN"?


inception101 trade, development, design
Report Abuse
Happywalker is not online. Happywalker
Joined: 05 Mar 2010
Total Posts: 3672
19 Mar 2016 02:41 PM
spawn(f)

spawns function in own thread


Report Abuse
inception101 is not online. inception101
Joined: 19 Dec 2010
Total Posts: 738
19 Mar 2016 02:50 PM
On the wiki, it says that spawn(f) calls two parameters on default (delay time and server-side time). Unless I needed these values, would I just set 2 parameters to fill and then the needed parameters following the "fillers"?


inception101 trade, development, design
Report Abuse
LegendaryAccount is not online. LegendaryAccount
Joined: 02 Jun 2010
Total Posts: 13193
19 Mar 2016 02:56 PM
spawn(function()
while wait() do
print("Foo")
end
end)

while true do
print("Doo")
end


Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
19 Mar 2016 03:07 PM
spawn(function()
-- Do your code
end);

-- Code
-- Code,
-- More code.
wait() -- Your spawn() now runs in this time
-- More code,
-- Your spawn() has stopped now.
Report Abuse
inception101 is not online. inception101
Joined: 19 Dec 2010
Total Posts: 738
19 Mar 2016 03:22 PM
Well I appreciate all of the help, but I decided to go with a coroutine function and put a while loop suspended by a yield to repeat the process, but I don't think the yield is working as the loop keeps playing.

func = coroutine.wrap(function()
while wait() do
print("A")
wait(1)
coroutine.yield()
end
end)

Is my syntax wrong?


inception101 trade, development, design
Report Abuse
62GB is not online. 62GB
Joined: 03 Oct 2011
Total Posts: 4157
19 Mar 2016 03:39 PM
func = coroutine.wrap(function()
while wait() do
print("A")
wait(1)
input = coroutine.yield()
end
end)
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
19 Mar 2016 04:01 PM
Meanwhile, in the world of DOING THINGS THE RIGHT WAY USING SPAWN
Report Abuse
inception101 is not online. inception101
Joined: 19 Dec 2010
Total Posts: 738
19 Mar 2016 05:28 PM
eLunate, I don't mean to offend you, but I don't understand at all how to use spawn. I tried but I got errors... a lot of errors XD.


inception101 trade, development, design
Report Abuse
riftg is not online. riftg
Joined: 24 Nov 2008
Total Posts: 1980
19 Mar 2016 06:05 PM
2D2A8595

@62GB

jk I don't mean it

RiftG never sleeps
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
19 Mar 2016 06:56 PM
spawn() just runs the function inside the time that your script is waiting. It's better because there's less race conditions for resources.
Report Abuse
riftg is not online. riftg
Joined: 24 Nov 2008
Total Posts: 1980
19 Mar 2016 07:03 PM
144FAFA7

@LegendaryAccount
I do not understand.
jk I don't mean it

RiftG never sleeps
Report Abuse
inception101 is not online. inception101
Joined: 19 Dec 2010
Total Posts: 738
19 Mar 2016 07:17 PM
eLunate, I took your advice and started using spawn() and I got it to work, but now I need to pass parameters to the function as well. Here's what I got so far...

Deb = true

function test() --animation
Deb = false
for i = 1,10 do
wait(.25)
print("Yolo")
end
Deb = true
end



while wait(1) do --Awareness loop
x = math.random(1,2)
if x == 1 and Deb then
spawn(test)
else
print("Not Yolo")
end
end

This isn't my actual code, but it resembles the procedure to an accurate extent. I just don't want people stealing my code :P.


inception101 trade, development, design
Report Abuse
62GB is not online. 62GB
Joined: 03 Oct 2011
Total Posts: 4157
19 Mar 2016 07:20 PM
Im pretty sure you can do,

spawn(test(4))

but idk.
Report Abuse
inception101 is not online. inception101
Joined: 19 Dec 2010
Total Posts: 738
19 Mar 2016 07:32 PM
No because spawn(f) by default returns 2 arguments (delay time and server age).


inception101 trade, development, design
Report Abuse
riftg is not online. riftg
Joined: 24 Nov 2008
Total Posts: 1980
19 Mar 2016 07:44 PM
711BAE52

@inception101
Enough about me, let's talk about my dress.What is stopping you? Check back later and see if I learn the answer to that one. Tell me one of your favorite possessions.
jk I don't mean it

RiftG never sleeps
Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
19 Mar 2016 08:00 PM
You can't pass arguments to spawn functions.


Report Abuse
inception101 is not online. inception101
Joined: 19 Dec 2010
Total Posts: 738
19 Mar 2016 08:10 PM
Well I could create a variable and pass values through that, correct?


inception101 trade, development, design
Report Abuse
riftg is not online. riftg
Joined: 24 Nov 2008
Total Posts: 1980
19 Mar 2016 09:03 PM
D1E42556

@eLunate
I am very logical and rational. Ok I'll try not to do US3 COROUT1N3S too much. Why not? Do you always express yourself that way? I haven't heard anything like that before.
jk I don't mean it

RiftG never sleeps
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • 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