How to put a limit on many children can be in a scrolling frame

Hello, I have a sign that communicates across all servers when someone does something and clones a frame into this scrolling frame. So obviously it can get full pretty easy. How could I go about putting a limit on how many frames can be cloned into it? I though about having a script that whenever a new frame is added it checks how many are in the scrolling frame and deletes the oldest one but I’m not sure how id be able to tell what ones the oldest.
image

you can perhaps use tables to determine the oldest frame/template.

local CreatedFrames={} 
-- whenever you create a new frame, do 
table.insert(CreatedFrames,NewFrame)

-- then when you want to get the oldest one
local oldest
for i=1, #CreatedFrames do 
    local x=CreatedFrames[i]
    if x then 
       oldest=x 
       table.remove(CreatedFrames,i)
       break 
    end
end
oldest:Destroy() -- destroy the oldest frame

whenever you insert a value in a table, it gets added to the end of the existing table.
for example if we have a table {"Apple","Banana"} and we table.insert("Orange")
our new table will be {"Apple","Banana","Orange"}

thus, you in all cases, the first value of the table will be the oldest frame.
thats what we did here in the script, we got the first value of the table

1 Like

That’s genius it worked great thank you.

1 Like

This can be made more efficient. Becuse you are removing the oldest from the table and inserting the newest into the table, the oldest frame will always be CreatedFrames[1]

local CreatedFrames={} 
-- whenever you create a new frame, do 
table.insert(CreatedFrames, NewFrame)

-- then when you want to get the oldest one
local oldest = CreatedFrames[1]
table.remove(CreatedFrames, 1)

oldest:Destroy() -- destroy the oldest frame

@0k_Jackk update the code to this

1 Like

if for any chance the oldest frame has been destroyed, the script will error.
my script will work around such errors, and get the next oldest frame.

1 Like

no ur script works the exact same way mine does. I understand the error ur trying to avoid but ur script does not even avoid the error. The frame will not be garbage collected (meaning the frame will always exsist in the table) because a refrence exsists in the list, meaning that if a frame was added to the table, it will always be in the table until it is removed from it, making this check useless:
Screenshot 2023-09-17 at 12.57.31 AM

Solution to the issue ur describing:

local CreatedFrames={} 
-- whenever you create a new frame, do 
table.insert(CreatedFrames,NewFrame)

local oldest
for i=1, #CreatedFrames do 
	local Frame = CreatedFrames[i]
	if Frame and Frame.Parent then 
		oldest = Frame 
		
		--Remove all the destroyed frames from current index
		for x = i, 1, -1 do
			table.remove(CreatedFrames, x)
		end
		
		break 
	end
end

oldest:Destroy()

@0k_Jackk use this updated code instead

1 Like

Sorry I didnt get the chance to test it till now but I was just wondering how I can run it after theres already 10 frames so every frame doesnt get deleted.

I quickly wrote this code, but im going to an event for ~6 hours. Ill make it more efficient when I get back:

--Functions
local FrameCollector = {}
local function RemoveOldestFrame(TableOfFrames)
	local oldest
	for i=1, #FrameCollector do 
		local Frame = FrameCollector[i]
		if Frame and Frame.Parent then 
			oldest = Frame 

			--Remove all the destroyed frames from current index
			for x = i, 1, -1 do
				table.remove(FrameCollector, x)
			end

			break 
		end
	end
	if oldest then oldest:Destroy() end
end

local function AddFrameToCollector(Frames)
	if #FrameCollector >= 10 then
		for i = #FrameCollector, 10, -1 do
			RemoveOldestFrame(FrameCollector)
		end
	end
	table.insert(FrameCollector,Frames)
end

--Usage:
local Frame = Instance.new("Frame")
AddFrameToCollector(Frame)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.