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

[Solved]Why is my while loop running only once and then never again?

Asked by 6 years ago
Edited 6 years ago

Here is the code:

local function runDropper(dropper)

    spawn(function()

        local loopWaitTime = dropper:WaitForChild("LoopWaitTime")
        local oreValue = dropper:WaitForChild("OreValue")
        local dropperPart = dropper:WaitForChild("DropperPart")

        if loopWaitTime and oreValue and dropperPart then

            local waitTime = loopWaitTime.Value
            local oreToClone = Instance.new("Part")
            local loopedTimes = 0

            while true do 

                wait(2)

                local newOre = oreToClone:Clone()

                newOre.Name = "Ore"
                newOre.Parent = dropper
                newOre.Size = Vector3.new(1, 1, 1)
                newOre.Position = dropperPart.Position

                game.Debris:AddItem(newOre, 20)

                local cash = Instance.new("NumberValue")

                cash.Name = "Cash"
                cash.Parent = newOre
                cash.Value = oreValue.Value

                loopedTimes = loopedTimes + 1

                print(loopedTimes)

            end 
        end
    end)
end

return runDropper

when I print loopedTimes it only prints: 1 Could someone tell me what is wrong? I have no idea. Thanks!

0
Try adding the wait() between the while and do royee354 129 — 6y
0
What? That won't solve anything. theCJarmy7 1293 — 6y
0
theCJarmy7 "while wait() do" is a shorthand of "while true do wait()" considering wait() only yeilds the code and not really anything else fanofpixels 718 — 5y
0
if ANY error happens in the loop the script stops and the loop does not go anymore fanofpixels 718 — 5y
0
I figured out the problem. That is why it says [Solved] User#21908 42 — 5y

Answer this question