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:
01 | wait( 1 ) |
02 | workspace:WaitForChild( "PartStorage" ) |
03 | while 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 - Vector 3. new( 0 , 1 , 0 ) |
10 | part.Size = Vector 3. new( 1 , 1 , 1 ) |
11 | part.LeftSurface = ( "Studs" ) |
12 | part.RightSurface = ( "Studs" ) |
13 | part.FrontSurface = ( "Studs" ) |
14 | part.BackSurface = ( "Studs" ) |
15 | part.TopSurface = ( "Studs" ) |
One option is to use Spawn, but coroutines are generally better, because they do not delay.
01 | wait( 1 ) |
02 | workspace:WaitForChild( "PartStorage" ) |
03 | while 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 - Vector 3. new( 0 , 1 , 0 ) |
10 | part.Size = Vector 3. new( 1 , 1 , 1 ) |
11 | part.LeftSurface = ( "Studs" ) |
12 | part.RightSurface = ( "Studs" ) |
13 | part.FrontSurface = ( "Studs" ) |
14 | part.BackSurface = ( "Studs" ) |
15 | part.TopSurface = ( "Studs" ) |
You can use the spawn
function begin running a function "in the background".
It would look like this:
1 | spawn( function () |
2 | while true do |
3 | part.BrickColor = BrickColor.Random() |
4 | wait( 0.5 ) |
5 | end |
6 | end ) |
However, this isn't very efficient. Lots of spawn
s 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:
01 | spawn( 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 |
08 | end ) |
09 |
10 | while true do |
11 | wait( 2 ) |
12 | local part ..... |
A simple delay functiom should work:
01 | wait( 1 ) |
02 | workspace:WaitForChild( "PartStorage" ) |
03 | while 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 - Vector 3. new( 0 , 1 , 0 ) |
10 | part.Size = Vector 3. new( 1 , 1 , 1 ) |
11 | part.LeftSurface = ( "Studs" ) |
12 | part.RightSurface = ( "Studs" ) |
13 | part.FrontSurface = ( "Studs" ) |
14 | part.BackSurface = ( "Studs" ) |
15 | part.TopSurface = ( "Studs" ) |
As I do agree with aquathorn321 I would use coroutine.wrap() instead
01 | wait( 1 ) |
02 | workspace:WaitForChild( "PartStorage" ) |
03 | local parts = { } |
04 | workspace.PartStorage.ChildRemoved:connect( function (par) |
05 | -- check if part is in table |
06 | local isInTable = false |
07 | local gtPart = nil |
08 | for _, v in pairs (parts) do |
09 | if v.name = = par.Name then |
10 | isInTable = true |
11 | gtPart = v |
12 | end |
13 | end |
14 |
15 | if isInTable then |