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 5 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?

local model = workspace.Model

while #model:GetChildren() <= 20 do
    local part = Instance.new("Part")
    local cd = Instance.new("ClickDetector", part)
    local xpos = math.random(-100,100)
    local height = 20
    local zpos = math.random(-100,100)
    local model = game.Workspace.Model
    local children = model:GetChildren()
        part.Parent = workspace.Model
        part.Position = Vector3.new(xpos,height,zpos)
        print(#children)
        wait(2)

cd.MouseClick:Connect(function(player)
    local score = player:WaitForChild("leaderstats"):WaitForChild("Score")
        score.Value = score.Value + 1
            part:Destroy()

            if score.Value > 10 then
                score.Value = score.Value + 1
            end

            if score.Value > 20 then
                score.Value = score.Value + 1

        end
    end)
end
0
because it is only doing the number of stuff inside model Nooberton_1 9 — 5y
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 — 5y

3 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 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

local model = workspace.Model

while true do
    if #model:GetChildren() < 20 then
    local part = Instance.new("Part")
    local cd = Instance.new("ClickDetector", part)
    local xpos = math.random(-100,100)
    local height = 20
    local zpos = math.random(-100,100)
    local model = game.Workspace.Model
    local children = model:GetChildren()
        part.Parent = workspace.Model
        part.Position = Vector3.new(xpos,height,zpos)
        print(#children)
        wait(2)

cd.MouseClick:Connect(function(player)
    local score = player:WaitForChild("leaderstats"):WaitForChild("Score")
        score.Value = score.Value + 1
            part:Destroy()

            if score.Value > 10 then
                score.Value = score.Value + 1
            end

            if score.Value > 20 then
                score.Value = score.Value + 1

        end
    end)
end
0
oops Nooberton_1 9 — 5y
0
that's still wrong, that's an infinite loop with no way to break it SteamG00B 1633 — 5y
Ad
Log in to vote
0
Answered by 5 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.

function PartAdder()
    local count = 0

    for _,v in pairs(workspace:GetChildren()) do
        if v:IsA("Part") then
            count = count + 1
        end
    end

    if count < 20 then
        for _ = 1,20-count do
            local inst = Instance.new("Part")
            inst.Parent = workspace
        end
    end
end

workspace.ChildRemoved:Connect(PartAdder)

PartAdder()

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 — 5y
0
Also, her parts are not in the workspace SteamG00B 1633 — 5y
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 — 5y
0
Might have again be a mistake by me, the code would not continue to produce more parts when parts are removed. top500widowxd 23 — 5y
Log in to vote
0
Answered by
SteamG00B 1633 Moderation Voter
5 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:

local m = game.Workspace.Model

function createParts(model)
    while #model:GetChildren() <= 20 do
        local part = Instance.new("Part")
        local cd = Instance.new("ClickDetector", part)
        local xpos = math.random(-100,100)
        local height = 20
        local zpos = math.random(-100,100)
        local children = model:GetChildren()
        part.Parent = workspace.Model
        part.Position = Vector3.new(xpos,height,zpos)
        print(#children)
        wait(2)
    end
    killParts(model)
end

function killParts(model)
    while #model:GetChildren() > 0 do
        model.Part.ClickDetector.MouseClick:Connect(function(player)
            local score = player:WaitForChild("leaderstats"):WaitForChild("Score")
            score.Value = score.Value + 1
            model.part:Destroy()

            if score.Value > 10 then
                score.Value = score.Value + 1
            end

            if score.Value > 20 then
                score.Value = score.Value + 1
            end
        end)
    end
    createParts()
end

createParts(m)
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 — 5y

Answer this question