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

Why has my while loop stopped looping?

Asked by 6 years ago

I did this a while back so I forgot how it worked, but my intended purpose was for the while loop to create parts until there is 20 parts in the game and stops. Then continues again when there is less than 20 parts in the game.

How can I restart the loop again?

01local model = workspace.Model
02 
03while #model:GetChildren() <= 20 do
04    local part = Instance.new("Part")
05    local cd = Instance.new("ClickDetector", part)
06    local xpos = math.random(-100,100)
07    local height = 20
08    local zpos = math.random(-100,100)
09    local model = game.Workspace.Model
10    local children = model:GetChildren()
11        part.Parent = workspace.Model
12        part.Position = Vector3.new(xpos,height,zpos)
13        print(#children)
14        wait(2)
15 
View all 30 lines...
0
because it is only doing the number of stuff inside model Nooberton_1 9 — 6y
0
I'm glad you asked this because I love creating recursive loops, and this is probably the perfect problem for that solution SteamG00B 1633 — 6y

3 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

the reason it is not looping is because it is only looping the amount of Children is inside model

here is the fix

01local model = workspace.Model
02 
03while true do
04    if #model:GetChildren() < 20 then
05    local part = Instance.new("Part")
06    local cd = Instance.new("ClickDetector", part)
07    local xpos = math.random(-100,100)
08    local height = 20
09    local zpos = math.random(-100,100)
10    local model = game.Workspace.Model
11    local children = model:GetChildren()
12        part.Parent = workspace.Model
13        part.Position = Vector3.new(xpos,height,zpos)
14        print(#children)
15        wait(2)
View all 31 lines...
0
oops Nooberton_1 9 — 6y
0
that's still wrong, that's an infinite loop with no way to break it SteamG00B 1633 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

This code will accomplish what you are hoping for (making sure workspace always has at least 20 parts). Hopefully you can visualize how to add specific things to these parts on your own.

01function PartAdder()
02    local count = 0
03 
04    for _,v in pairs(workspace:GetChildren()) do
05        if v:IsA("Part") then
06            count = count + 1
07        end
08    end
09 
10    if count < 20 then
11        for _ = 1,20-count do
12            local inst = Instance.new("Part")
13            inst.Parent = workspace
14        end
15    end
16end
17 
18workspace.ChildRemoved:Connect(PartAdder)
19 
20PartAdder()

Essentially what we're doing is hooking a function which will make sure workspace has at least 20 parts to the inherited ChildRemoved event of workspace. This way, whenver a child is removed, it will check to make sure that 20 parts still exist within workspace.

We then run the function once to ensure that workspace has at least 20 parts when the game starts up.

If you have questions feel free to ask, I may not have explained this super well -- but it does work how you want.

0
That's a really difficult way to make 20 parts, and even then, you failed to include click detectors, and his score adder SteamG00B 1633 — 6y
0
Also, her parts are not in the workspace SteamG00B 1633 — 6y
0
Steam, as stated in my response - this is a blueprint, and it does not include the things which they are doing in their initial script. It is not complicated - it is simple and effective. You should always opt to use events over infinitely occuring loops if possible. SummerEquinox 643 — 6y
0
Might have again be a mistake by me, the code would not continue to produce more parts when parts are removed. top500widowxd 23 — 6y
Log in to vote
0
Answered by
SteamG00B 1633 Moderation Voter
6 years ago

Alright, so what it seems like what you wanted was to have 20 parts created and then let the player click all of them until they were gone, and then spawn a new 20 parts, so I basically gut your entire code and rewrote it with a simpler recursive design:

01local m = game.Workspace.Model
02 
03function createParts(model)
04    while #model:GetChildren() <= 20 do
05        local part = Instance.new("Part")
06        local cd = Instance.new("ClickDetector", part)
07        local xpos = math.random(-100,100)
08        local height = 20
09        local zpos = math.random(-100,100)
10        local children = model:GetChildren()
11        part.Parent = workspace.Model
12        part.Position = Vector3.new(xpos,height,zpos)
13        print(#children)
14        wait(2)
15    end
View all 38 lines...
0
Might have been my fault but, upon reaching 20 parts, my studio would freeze and there would be an error saying "Game Script Timeout". Clicking on the parts do nothing now. top500widowxd 23 — 6y

Answer this question