Repeat Until Breaking

Hi! I wrote this bit of code that does work as intended, as it’s supposed to generate 1-9 items on a random node. However, there’s this weird quirk where it repeats infinitely. It doesn’t happen repeatedly, just has a small chance of happening. Random of sorts, so I think I might have done something wrong with math.random?

-- local Nodes = script.Parent.nodes:GetChildren()

local items = script.Parent.items
items.Parent = game.Lighting

local Supply = items.Supply:GetChildren()

function UpdateNodes()

	local Generated = 1
	local Amount = math.random(1,9)
		local Chosen_node = Nodes[math.random(#Nodes)]
		local Supply_item = Supply[math.random(#Supply)]:Clone()
		Chosen_node.Transparency = 1
		Supply_item.Parent = Chosen_node
		Supply_item.CFrame = Chosen_node.CFrame *,0.2,0)
		Generated = Generated + 1
		print("ITEM SPAWNED AT: " .. Chosen_node.Name)
		print("Amount Generated:" .. Generated)
	until Generated == Amount


If someone could point me in the right direction that would be great. Thanks ahead of time!

Wait, I think the issue was that I had started at 1. So, by adding 1 I eventually got to 10, which would cause the loop to run indefinitely. Sorry! I shouldn’t be allowed near numbers.

1 Like