Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

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

1 comment

0

I happen to know that there are four purchasable items.

This discussion is preserved as it was. New answers, comments, and votes are closed.