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: Run 2 loops simultaneously in one script?

Previous Thread :: Next Thread 
Thurston_Morris is not online. Thurston_Morris
Joined: 05 Jan 2014
Total Posts: 407
02 Nov 2016 07:34 PM
how
Report Abuse
Weirdraidercs35 is online. Weirdraidercs35
Joined: 14 May 2015
Total Posts: 2275
02 Nov 2016 07:37 PM
delay or spawn


im an mlg
Report Abuse
Thurston_Morris is not online. Thurston_Morris
Joined: 05 Jan 2014
Total Posts: 407
02 Nov 2016 07:38 PM
what?
Report Abuse
Wowgnomes is online. Wowgnomes
Joined: 27 Sep 2009
Total Posts: 26255
02 Nov 2016 07:38 PM
while true do
spawn(function()
while true do
wait(2)
print("I LOVE GNOMES")
end
end)
wait(1)
print("I LOVE GNOMES MORE AND SAY IT TWICE AS FAST")
end


Report Abuse
newcoolman2 is not online. newcoolman2
Joined: 01 Apr 2010
Total Posts: 2637
02 Nov 2016 07:40 PM
wiki.roblox.com/index.php?title=Beginners_Guide_to_Coroutines

wiki.roblox.com/index.php?title=Global_namespace/Functions_specific_to_ROBLOX#Spawn


Report Abuse
Weirdraidercs35 is online. Weirdraidercs35
Joined: 14 May 2015
Total Posts: 2275
02 Nov 2016 07:41 PM
basically maks a new thread for the loop to run on


im an mlg
Report Abuse
3027474 is not online. 3027474
Joined: 30 Dec 2008
Total Posts: 1636
02 Nov 2016 08:02 PM
Coroutines
Report Abuse
Wowgnomes is online. Wowgnomes
Joined: 27 Sep 2009
Total Posts: 26255
02 Nov 2016 09:08 PM
coroutines are not the best way for most things


Report Abuse
3027474 is not online. 3027474
Joined: 30 Dec 2008
Total Posts: 1636
02 Nov 2016 09:29 PM
If you have a different solution I would love to hear it
Report Abuse
Wowgnomes is online. Wowgnomes
Joined: 27 Sep 2009
Total Posts: 26255
02 Nov 2016 09:32 PM
i posted my solution...


Report Abuse
3027474 is not online. 3027474
Joined: 30 Dec 2008
Total Posts: 1636
02 Nov 2016 09:37 PM
That wouldn't work depending on what he's trying to do. I'm assuming he's trying to run two loops at the same time because one probably effects the condition of the other.
I've had to do that a couple times.
Report Abuse
Wowgnomes is online. Wowgnomes
Joined: 27 Sep 2009
Total Posts: 26255
02 Nov 2016 09:44 PM
local gnome1 = 0
local gnome2 = 0
while true do
spawn(function()
while true do
wait(2)
gnome2 = gnome2 + 1
end
end)
wait(1)
gnome1 = gnome1 + 1
print(gnome1 .. " has twice as many as " .. gnome2)
end

how does this not work lol


Report Abuse
Salinas23 is not online. Salinas23
Joined: 28 Dec 2008
Total Posts: 37141
02 Nov 2016 11:45 PM
wouldnt that do just the same as


local gnome1 = 0
local gnome2 = 0

while true do
wait(1)
gnome1 = gnome1+1
wait(1)
gnome2 = gnome2+1
gnome1 = gnome1+1
end



I have a spider on my back.
Report Abuse
Wowgnomes is online. Wowgnomes
Joined: 27 Sep 2009
Total Posts: 26255
02 Nov 2016 11:55 PM
Yeah, but you could change what the script does to make it actually useful..
op just asked how you can run 2 loops simultaneously, and I used an example that was easy to follow


Report Abuse
Salinas23 is not online. Salinas23
Joined: 28 Dec 2008
Total Posts: 37141
03 Nov 2016 12:00 AM
how 'change' what the spawned function does??

like

while true do
spawn(function()
while wait(5) do
local plr = game.Players[math.random(1, #game.Players:GetPlayers())]
do stuff
wait()
end
end)
wait(1)
print("whatever")
end

I have a spider on my back.
Report Abuse
Wowgnomes is online. Wowgnomes
Joined: 27 Sep 2009
Total Posts: 26255
03 Nov 2016 12:06 AM
while true do
spawn(function()
while wait(5) do
local plr = game.Players[math.random(1, #game.Players:GetPlayers())]
print(plr.Name)
wait(30)
end
end)
wait(1)
print("whatever")
end

exactly


Report Abuse
Sen_Takatsuki is not online. Sen_Takatsuki
Joined: 07 Aug 2013
Total Posts: 673
03 Nov 2016 04:23 AM
Use coroutines.
coroutine.wrap(function()
-- Code Here
end)()


#code Signature_mod = require(DataModel['sm_Data']) if Signature_mod['xThe..']['RbxDev_Info'] ~= 'Too Lazy To Try Again After A Year' then print('You finally did it lol') end
Report Abuse
Exfiltrate is not online. Exfiltrate
Joined: 29 Mar 2013
Total Posts: 10917
03 Nov 2016 09:30 AM
21
Report Abuse
Trioxide is not online. Trioxide
Joined: 29 Mar 2011
Total Posts: 32902
03 Nov 2016 10:47 AM
@wowgnomes

You're starting a new loop every x seconds.
Report Abuse
MiniNob is not online. MiniNob
Joined: 14 May 2013
Total Posts: 822
03 Nov 2016 11:31 AM
MAKE ONE MAIN LOOP AND COMBINE BOTH LOOPS
Report Abuse
Wowgnomes is online. Wowgnomes
Joined: 27 Sep 2009
Total Posts: 26255
03 Nov 2016 11:42 AM
@trioxide, good catch

spawn(function()
while wait(5) do
local plr = game.Players[math.random(1, #game.Players:GetPlayers())]
print(plr.Name)
wait(30)
end
end)
spawn(function()
wait(1)
print("whatever")
end)


Report Abuse
Z_V is online. Z_V
Joined: 11 Dec 2012
Total Posts: 2638
03 Nov 2016 01:31 PM
thread = function(...)
return coroutine.resume(coroutine.create(...))
end

thread(function()
while wait(2) do
print("First loop")
end
end)

thread(function()
while wait(2) do
print("Second loop")
end
end)


Report Abuse
Wowgnomes is online. Wowgnomes
Joined: 27 Sep 2009
Total Posts: 26255
03 Nov 2016 05:06 PM
coroutines are resource intensive and not the right solution for most cases where it is used


Report Abuse
Wunder_Wulfe is online. Wunder_Wulfe
Joined: 13 Sep 2016
Total Posts: 8356
03 Nov 2016 05:10 PM
A) delay(0,func)

B) spawn(func)

C) coroutine.wrap(func)()

D) (least intensive)
local b=Instance.new("BindableEvent")
b.Event:connect(func)

b:Fire()
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