Help with limiting clone children

Hi,

I wanted to make a donation game(1 player game) and I wanted to add fake bot chats because the theme is streaming online.

I did make a script where it deletes a child when it reaches above 17 children but it doesn’t work and instead gives me two errors.


(ChatContent is the parent of the children)
I don’t know what to do since I have never seen this error before.

BotChatScript - duplicates fake comment and parent to ChatContent

local mainFrame = script.Parent
local temp = script:WaitForChild("Template")
local chatContent = mainFrame:WaitForChild("ChatContent")

local Players = game:GetService("Players")
local plr = Players.LocalPlayer
local userName = plr.Name
local userId = Players:GetUserIdFromNameAsync(userName)
local friendPages = Players:GetFriendsAsync(userId)

local usernames = {}
local com = {
	"pls say my name",
	"keep up the good work queen!",
	"yey live again",
	"amazing work",
	"<3",
	"poggers",
	"we do a trolling",
	"how do i donate?"
}

local minTime = 1
local maxTime = 3

local function iterPageItems(pages)
	return coroutine.wrap(function()
		local pagenum = 1
		while true do
			for _, item in ipairs(pages:GetCurrentPage()) do
				coroutine.yield(item, pagenum)
			end
			if pages.IsFinished then
				break
			end
			pages:AdvanceToNextPageAsync()
			pagenum = pagenum + 1
		end
	end)
end

for item, pageNo in iterPageItems(friendPages) do
	table.insert(usernames, item.Username)
end

local function clone(name, com)
	local clone = temp:Clone()
	
	if clone then
		clone.Parent = chatContent
		clone:WaitForChild("Username").Text = name .. ":"
		clone:WaitForChild("Comment").Text = com
	end
end

local function radCom()
	local comment = {}

	for c in pairs(com) do
		table.insert(comment, c)
	end

	local choiceIndex = math.random(1,#comment)
	local choiceKey = comment[choiceIndex]
	local choice = com[choiceKey]

	return choice
end

local function radUse()
	local username = {}

	for user in pairs(usernames) do
		table.insert(username, user)
	end

	local choiceIndex = math.random(1,#username)
	local choiceKey = username[choiceIndex]
	local choice = usernames[choiceKey]

	clone(choice, radCom())
end

while wait(math.random(minTime,maxTime)) do
	radUse()
end

Inside the template(the comments), there is a username and comment I used the player’s friend as the username and a table(array) as the comment. The template duplicates between 1-3 seconds since it duplicates I wanted to delete a child and replace a new one.

LimitScript - deletes a child when it reaches above 17

local mainFrame = script.Parent
local chatContent = mainFrame:WaitForChild("ChatContent")

local limit = 17

local function destroy(child)
	if #chatContent:GetChildren() <= limit then return end
	child:Destroy()
end

chatContent.ChildAdded:Connect(destroy)

for _, child in ipairs(chatContent:GetChildren()) do
	destroy(child)
end

Here is the explorer and the error for more context:
Explorer
image

Error
image

also, I added a feature where the player can also chat I hope it doesn’t make an error : (
feel free to comment if there are questions
sorry if the forum is messy tried my best

Thanks in advance