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

why is this loop only running once and not finishing the rest of the script?

Asked by 6 years ago
Edited 6 years ago

Here is the code:

if copy.ItemType.Value == "Dropper" then

                    copy.Parent = tycoonBaseplate:FindFirstChild("PurchasedItems")

                    spawn(function()

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

                        while wait(loopWaitTime.Value) do 

                            local newOre = Instance.new("Part")

                            newOre.Name = "Ore"
                            newOre.Parent = dropper
                            newOre.Size = dropperPart.Size
                            newOre.Position = dropperPart.Position

                            game.Debris:AddItem(newOre, 25)

                            local cash = Instance.new("NumberValue")

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

                        end
                    end)

2 answers

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

Your module script will never get to the return line so the could will block at the while loop.

Example:-

-- script
print('start')
print(require(script.ModuleScript))
print('done')

-- module
while wait(1) do
    print('a')
end

return true -- cannot get to the return

You can use spawn to create a new thread around your while loop.

Example:-

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

-- run codeas thread
spawn(function()

-- you need to think about how you will stop the loop
    while wait(loopWaitTime.Value) do 

        local newOre = Instance.new("Part")

        newOre.Name = "Ore"
        newOre.Parent = dropper
        newOre.Size = dropperPart.Size
        newOre.Position = dropperPart.Position

        game.Debris:AddItem(newOre, 25)

        local cash = Instance.new("NumberValue")

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

    end
end)

return true

I hope this helps.

Ad
Log in to vote
0
Answered by 6 years ago

There's either only 1 purchasable item, or it errors somewhere.

0
I happen to know that there are four purchasable items. User#21908 42 — 6y

Answer this question