Cloning Item doubling on death

I made a script where it clones a crate into the players shop, but for some reason it douplicates so there is 2 of each crate when the player dies. It is normal and there is only one of each create when the player joins, there is no error messages.

I tried adding a part of the script where checks how many things are inside the table, but it does not work.

local crateButtons = {}

for _, crate in pairs(crates:GetChildren()) do
	
	local crateProperties = require(crate)
	
	local newBtn = crateButtonTemplate:Clone()
	newBtn.Name = crate.Name
	newBtn.CrateName.Text = crate.Name
	newBtn.CrateImage.Image = crateProperties["Image"]
	
	newBtn.MouseButton1Click:Connect(function()
		
		if selectedCrate.CrateName.Text ~= crate.Name then
			
			selectedCrate.CrateName.Text = crate.Name
			selectedCrate.CrateImage.Image = crateProperties["Image"]
			selectedCrate.UnboxButton.Text = "$" .. crateProperties["Price"]
			
			local rarities = {}
			for rarityName, chance in pairs(crateProperties["Chances"]) do
				table.insert(rarities, {rarityName, chance})
			end
			table.sort(rarities, function(a, b)
				return rarityProperties[a[1]].Order.Value <rarityProperties[b[1]].Order.Value
			end)
			
			local raritiesText = ""
			for _, rarity in pairs(rarities) do
				local color = rarityProperties[rarity[1]].Color.Value
				color = {R = math.round(color.R*255); G = math.round(color.G*255); B = math.round(color.B*255)}
				raritiesText = raritiesText .. '<font color="rgb(' .. color.R .. ',' .. color.G .. ',' .. color.B .. ')">' .. rarity[1] .. ': <b>' .. rarity[2] .. '%</b></font><br />'
			end
			selectedCrate.RaritiesText.RichText = true
			selectedCrate.RaritiesText.Text = raritiesText
			
			for _, child in pairs(selectedCrate.ItemsList.List:GetChildren()) do
				if child:IsA("Frame") then
					child:Destroy()
				end
			end
			
			local unboxableItems = crateProperties["Items"]
			table.sort(unboxableItems, function(a, b)
				return 
					(rarityProperties[items:FindFirstChild(a, true).Parent.Name].Order.Value < rarityProperties[items:FindFirstChild(b, true).Parent.Name].Order.Value)
					or (rarityProperties[items:FindFirstChild(a, true).Parent.Name].Order.Value == rarityProperties[items:FindFirstChild(b, true).Parent.Name].Order.Value)
					and (a < b)
			end)
			
			for _, unboxableItemName in pairs(unboxableItems) do
				local newItemFrame = selectedCrateItemTemplate:Clone()
				newItemFrame.ItemName.Text = unboxableItemName
				newItemFrame.ItemName.TextColor3 = rarityProperties[items:FindFirstChild(unboxableItemName, true).Parent.Name].Color.Value
				
				local itemModel = Instance.new("Model")
				
				for _, child in pairs(items:FindFirstChild(unboxableItemName, true):GetChildren()) do
					if not (child:IsA("Script") or child:IsA("LocalScript") or child:IsA("ModuleScript") or child:IsA("Sound")) then
						child:Clone().Parent = itemModel
					end
				end
				
				itemModel:PivotTo(CFrame.new() * CFrame.Angles(math.rad(-39), 0, 0))
				
				itemModel.Parent = newItemFrame.ItemImage
				
				local currentCamera = Instance.new("Camera")
				currentCamera.CFrame = CFrame.new(Vector3.new(-itemModel:GetExtentsSize().Y*0.7, 0, 0), itemModel:GetPivot().Position + Vector3.new(0, -0.1, 0))
				currentCamera.Parent = newItemFrame.ItemImage
				newItemFrame.ItemImage.CurrentCamera = currentCamera
				
				newItemFrame.Parent = selectedCrate.ItemsList.List
			end
			
			selectedCrate.Visible = true
		end
	end)
	
	if #crateButtons == 8 then
		return
	else
		table.insert(crateButtons, {newBtn, crateProperties["Price"]})
	end

end

table.sort(crateButtons, function(a, b)
	return (a[2] < b[2]) or (a[2] == b[2] and a[1].Name < b[1].Name)
end)

for _, crateButton in pairs(crateButtons) do
	
	if #crateButtons == 8 then
		print(#crateButtons)
		return
	end
	crateButton[1].Parent = cratesList
end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.