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

How do I make a loop run if it has a loop within it?

Asked by
Scerzy 85
9 years ago

I have a loop that creates a brick every 2 seconds, but there's a loop within it that changes the brick color randomly. Is there any way of making both of them loop? For reference:

01wait(1)
02workspace:WaitForChild("PartStorage")
03while true do
04    wait(2)
05    local part = Instance.new("Part",workspace.PartStorage)
06    local cash = Instance.new("IntValue",part)
07    cash.Name = "Cash"
08    cash.Value = 50
09    part.CFrame = script.Parent.Dropper.CFrame - Vector3.new(0,1,0)
10    part.Size=Vector3.new(1,1,1)
11    part.LeftSurface=("Studs")
12    part.RightSurface=("Studs")
13    part.FrontSurface=("Studs")
14    part.BackSurface=("Studs")
15    part.TopSurface=("Studs")
View all 23 lines...

4 answers

Log in to vote
1
Answered by 9 years ago

One option is to use Spawn, but coroutines are generally better, because they do not delay.

01wait(1)
02workspace:WaitForChild("PartStorage")
03while true do
04    wait(2)
05    local part = Instance.new("Part",workspace.PartStorage)
06    local cash = Instance.new("IntValue",part)
07    cash.Name = "Cash"
08    cash.Value = 50
09    part.CFrame = script.Parent.Dropper.CFrame - Vector3.new(0,1,0)
10    part.Size=Vector3.new(1,1,1)
11    part.LeftSurface=("Studs")
12    part.RightSurface=("Studs")
13    part.FrontSurface=("Studs")
14    part.BackSurface=("Studs")
15    part.TopSurface=("Studs")
View all 25 lines...
Ad
Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

You can use the spawn function begin running a function "in the background".

It would look like this:

1spawn(function()
2    while true do
3        part.BrickColor = BrickColor.Random()
4        wait(0.5)
5    end
6end)

However, this isn't very efficient. Lots of spawns happening will bog down all of your scripts.

Instead, you can get away with just one spawn, by updating all of the bricks at once:

01spawn(function()
02    while true do
03        for _, part in pairs(workspace:WaitForChild("PartStorage")) do
04            part.BrickColor = BrickColor.Random()
05        end
06        wait(0.5)
07    end
08end)
09 
10while true do
11    wait(2)
12    local part .....
Log in to vote
1
Answered by 9 years ago

A simple delay functiom should work:

01wait(1)
02workspace:WaitForChild("PartStorage")
03while true do
04    wait(2)
05    local part = Instance.new("Part",workspace.PartStorage)
06    local cash = Instance.new("IntValue",part)
07    cash.Name = "Cash"
08    cash.Value = 50
09    part.CFrame = script.Parent.Dropper.CFrame - Vector3.new(0,1,0)
10    part.Size=Vector3.new(1,1,1)
11    part.LeftSurface=("Studs")
12    part.RightSurface=("Studs")
13    part.FrontSurface=("Studs")
14    part.BackSurface=("Studs")
15    part.TopSurface=("Studs")
View all 23 lines...
Log in to vote
-1
Answered by 9 years ago

As I do agree with aquathorn321 I would use coroutine.wrap() instead

01wait(1)
02workspace:WaitForChild("PartStorage")
03local parts = {}
04workspace.PartStorage.ChildRemoved:connect(function(par)
05-- check if part is in table
06local isInTable = false
07local gtPart = nil
08for _, v in pairs(parts) do
09 if v.name == par.Name then
10isInTable = true
11gtPart = v
12end
13end
14 
15if isInTable then
View all 44 lines...

Answer this question