Script Check for Time Leaderboard

Can someone explain me how this script doesn’t delete the content inside the scrolling frame? bc it doubles all the time-

local function InsertTemplateFrame()
	
	for _, leaderboard in pairs(leaderboards) do

		local scrollingFrame = leaderboard.Leaderboard.LeaderboardGui.LeaderboardContainer.PlayersContainer

		if templateFrame.RankText.Text == "1" then

			for __, child in pairs(scrollingFrame:GetChildren()) do
				if child.ClassName == templateFrame.ClassName then
					child:Destroy() --// This part is what I need help with bc it doesn't destroy nor update globally, it only updates for 1 person and the rest glitches.
				end
			end
		end

		local newPlayerFrame = templateFrame:Clone()
		newPlayerFrame.Parent = scrollingFrame

		if scrollingFrame.AbsoluteWindowSize.Y >= scrollingFrame.AbsoluteCanvasSize.Y then
			scrollingFrame.ScrollingEnabled = false
		else
			scrollingFrame.ScrollingEnabled = true
		end

		local plr = game.Players:FindFirstChild(templateFrame.NameText.Text)

		if plr then

			local plrTimeValue = Leaderstats.GetTime(plr)

			newPlayerFrame.TimeText.Text = plrTimeValue.Value

			local connection = nil

			connection = plrTimeValue:GetPropertyChangedSignal("Value"):Connect(function()
				newPlayerFrame.TimeText.Text = plrTimeValue.Value
			end)

			newPlayerFrame.Destroying:Connect(function()
				connection:Disconnect()
			end)
		end
	end
end

You could try separating the function that destroys the frame and disconnects the connection. I’m not sure if this will work, but you could try something like this instead:

newPlayerFrame:Destroy()
connection:Disconnect()

If you remove the if statement, does it work?

hold on, I’ll try and see if your suggestion guys do work, I’ll just take a break from scripting and eat lunch I’m way past my time to eat lunch already :sob: @jasper53682

Sorry took me a bit to respond, I’ll try this tomorrow Than you for the suggestions <3