My queueing system isn't working as intended, I wonder why?

Hello everyone!

I was making some kind of “Production line” system, or a queue. Everything was going fine until I implemented queueing MULTIPLE things at once. At that point everything just fell apart.

Here’s my code

local function onProduce(player, object, productName)
	if object == building then
		local product = units:FindFirstChild(productName)
		
		if product then
			if #building.Queue:GetChildren() < 5 then
				local infoUnit = info[productName]
				local alloyPrice, energyPrice = infoUnit.Price.Alloy, infoUnit.Price.Energy
				
				local playerAlloy = player.Values.Alloy
				local playerEnergy = player.Values.Energy
				
				if playerAlloy.Value >= alloyPrice and playerEnergy.Value >= energyPrice then
					if alreadyProducing == false then
						productionQueue = {product}
					elseif alreadyProducing == true then
						table.insert(productionQueue, product)
					end
					
					alreadyProducing = true
					for i,obj in pairs(productionQueue) do
						
						local clone = obj:Clone()
						infoUnit = info[clone.Name]
						local timeDelay = infoUnit.ProductionTime
						
						playerAlloy.Value = playerAlloy.Value - alloyPrice
						playerEnergy.Value = playerEnergy.Value - energyPrice
						
						local objectVal = Instance.new("ObjectValue")
						objectVal.Parent = building.Queue
						objectVal.Name = productName
						objectVal.Value = clone
						
						if i == 1 then
							print('tweening')
							local ti = TweenInfo.new(infoUnit.ProductionTime, Enum.EasingStyle.Linear)
							building.Values.TimeNeeded.Value = infoUnit.ProductionTime
							building.Values.CurrentProgress.Value = 0
							
							local progress = tweenService:Create(building.Values.CurrentProgress, ti, {Value = infoUnit.ProductionTime})
							progress:Play(); progress:Destroy()
						end
						
						wait(timeDelay)
						
						building.Values.CurrentProgress.Value = 0
						building.Values.TimeNeeded.Value = 0
						
						local found = nil
						
						for _,v in pairs(building.Queue:GetChildren()) do
							if v:IsA("ObjectValue") and v.Value == clone then
								found = clone
								v:Destroy()
								break
							end
						end
						
						clone.Parent = workspace
						table.remove(productionQueue, i)
						
						if #productionQueue <= 0 then
							break
						end
					end
					alreadyProducing = false
				else
					print('insufficient funds')
				end
			else
				print('sorry no more space')
			end
		end
	end
end

I believe the problem was about the for loop restarting every event, but I’ve tried things like checking the amount of queues, and doing a debounce but all of that was in vain.

Please help

1 Like

I don’t know if this is confirmed, but you could probably use a coroutine for each queue you want to make.

1 Like

What? How would I use coroutines for this? Can you show me an example?

Like if we had a main thread

--> random code here
onProduce(plr,obj,productName)
coroutine.wrap(function()
  onProduce(plr,obj,productName)
end)()

I believe coroutines are routines that operate without disrupting the main thread.

2 Likes