How can I remove child with the lowest id (assigned in name)

Hi,
I have a frame with text labels, all these labels have their own ID
frameWithNumeredLabels

I need to write a script which will remove text label with the lowest ID when the count of these labels is bigger than 24 so I wrote this:

local data = frameData:GetChildren()

if #data > 24 then
	for index, value in ipairs(data) do
		data[index] = value.Name
	end
	frame[table.sort(data)[1]]:Destroy() -- attempt to index nil with number
end

I don’t know what’s wrong with this script, can someone help?

maybe try something like this you could also use sort but you would need to create another table variable and not use data again like you did in above code

local data = frameData:GetChildren()

if #data > 24 then
	local LowestItem
	for index, value in ipairs(data) do
		if not LowestItem or tonumber(value.Name) < tonumber(LowestItem.Name) then
			LowestItem = value
		end
	end
	if LowestItem then
		LowestItem:Destroy()
	end
end

i believe tablesort could be used like this

local data = frameData:GetChildren()

if #data > 24 then
	table.sort(data, function(a,b)
		return tonumber(a.Name) < tonumber(b.Name)
	end)
	data[1]:Destroy()  -- removes the first in table now that it is sorted
end
1 Like

Ur probably making a chat system. So:

local Items = {}
for i,v in pairs(frameData:GetChildren()) do
	table.insert(Items,v)
end
local function Update()
	if #Items > 24 then
		Items[1]:Destroy()
		table.remove(Items,1)
	end
end
Update()
frameData.ChildAdded:Connect(Update)
1 Like