Removing a frame after set number created

I’m tryna make a Kill feed and here’s what I currently got

local FeedCount = 0
local MaxFeed = 2 -- Set small for ease of testing (usually set at 6)

function updateFeed(action1, action2, image)
    local Feed = CreateFeed(action1, action2, image)
	
    FeedCount = FeedCount + 1
	
    Feed.Name = FeedCount
    Feed.Parent = KillFeed
	
    if FeedCount == MaxFeed then
	    FeedCount = 0
	    local Feed = KillFeed:FindFirstChild(FeedCount)
	    if Feed then
		    Feed:Destroy()
	    end		
    end
end

So my idea was, when this function is fired, it’d update the FeedCount by 1, named the frame as the FeedCount number. When there are 6 ‘Feeds’ and a 7th needs to be created, it should delete the oldest one to make room for the new one. Currently it created 1, then the 2nd one it deleted the old one and created a new one. Then after that it just keep creating new ones and never removing the old ones

3 Likes

im confused,Kill feed would become a nil variable or is Kill Feed an object

‘KillFeed’ is the parent frame for each individual feed. So Feed is the individual frames that go into the KillFeed frame. Once there’s 6 Feed frames tho I need to make room for the 7th, so need to delete the oldest one as there can only be 6. I know I could use ClipDescendants on the KillFeed frame, but that seems like a poor decision, as there could end up being hundreds of these frames on the screen at once even tho only 6 show

Don’t overcomplicate it or try to be clever.

local MAX_FEED = 6
local feed = {} -- Store all the elements

function updateFeed(action1, action2, image)

	-- At max
	if #feed == MAX_FEED then
		feed[1]:Destroy() -- Destroy element
		table.remove(feed, 1) -- Remove from list
	end

	-- Insert new element into list
	local newEntry = CreateFeed(action1, action2, image)
	table.insert(feed, newEntry)

end

You could also use :GetChildren(), but you don’t want to depend on the order being consistent (don’t try to be clever).

2 Likes

If you want a solution to fix your original attempt, you should keep track of the next element to remove.

local MAX_ELEMENTS = 6
local numElements = 0
local topElement = 1 -- The first element we remove

local function addElement()

	numElements = numElements + 1
	print("INSERTING:", numElements)
	-- create feed entry

	-- Over max, remove topElement
	if numElements > MAX_ELEMENTS then
		print("REMOVING:", topElement)
		killFeed:FindFirstChild(tostring(topElement)):Destroy() -- Destroy instance
		topElement = topElement + 1 -- Next in line
	end

end

You could also just do some math:

-- > 0 means you should remove
local topElement = numElements - MAX_ELEMENTS
1 Like

What if you make a table, insert the feedcount into the table, to find the first you only need to write the following script

local feedCountTable = {}
local MaxFeed = 2 -- Set small for ease of testing (usually set at 6)

function updateFeed(action1, action2, image)
    local Feed = CreateFeed(action1, action2, image)
	
    Feed.Name = #feedCountTable
    Feed.Parent = KillFeed
    table.insert(feedCountTable, Feed.Name)
	
    if #feedCountTable == MaxFeed then
	    table.remove(feedCountTable, 1)
	    local Feed = KillFeed:FindFirstChild(feedCountTable[1])
	    if Feed then
		    Feed:Destroy()
	    end		
    end
end

I haven’t troubleshooted the code myself so I hope this works!

1 Like