How would I detect if two children were added to an instance at the same time?

In my popup system, everytime a new popup is added to the player’s screen it’ll tween all the other popups upwards for readability.

script.Parent.PopupFrame.ChildAdded:Connect(function(instance : TextLabel)
	for i, popup in pairs(PopupSystem.Notifications) do
		if popup ~= instance then
			local tween = TweenService:Create(popup, TweenInfo.new(0.5), {Position = popup.Position + UDim2.new(0, 0, -0.08, 0)})
			tween:Play()
		end
	end
end)

However, if two popups are created at the same time, both of them appear at the same position, which is undesired. I was wondering if there was anyway to fix this issue or detect if two children were added at the same time.

You can create a queue of messages that need to be created and have your function for creating a message run until said queue is empty.

You could make it so the added children are added to the beginning of a queue(a fancy way to describe the table we’re using) and then have an iterator run through the rest of the heap. Here’s an example of what I mean:

local queue = {}

script.Parent.PopupFrame.ChildAdded:Connect(function(instance : TextLabel)
	table.insert(queue , 1, instance)

	for i, popup in ipairs(queue) do
		if i <= 1 then continue end
		
		local tween = TweenService:Create(popup, TweenInfo.new(0.5), {Position = popup.Position + UDim2.new(0, 0, -0.08, 0)})
		tween:Play()
	end
end)

yeah I tried this before but it still had the same issue, no idea if it’s because I implemented the queue wrong though

yeah, the problem was the implementation in my old queue, thank you for the help, this works