DataStore table re generating each time I join the game but it does saves when I leave

Hello, I have this script that saves the items the player owns, the items do save when leaving the game but when i join the values get generated again intead of loading the saved ones any idea why this is happening, thanks!

Part in script that re-generates the table
for i, v in pairs(p.OwnedItems:GetChildren()) do
	
		
	
		if v.ClassName == "BoolValue" then
			
			
			
				if data.OwnedItems[v.Name] then 
					
						---------if user has table
						
						p.OwnedItems[v.Name].Value = data.OwnedItems[v.Name]
						print("Owns "..v.Name)
						print(data.OwnedItems[v.Name])
							
							
				else if not data.OwnedItems[v.Name] then------------they re-generate each time no matter what 
							
					--------if dont have item on table generate it 
						
					table.insert(data.OwnedItems, v.Name)
					data.OwnedItems[v.Name] = false
					
					print("Generated Item  "..v.Name)
					print(data.OwnedItems[v.Name])
					
				
				end


			end
		
		
		
		else
			
			p.OwnedItems.EquippedItem.Value = data.EquippedItem
			
		end
		
		
		
	end 

Full Script
-- services
local Players = game:GetService("Players")

local DataService = game:GetService("DataStoreService")
local PlayerData = DataService:GetDataStore("datastore1")

-- variables
local ServerData = {}
local SavingFor = {}

-- functions


local function newData()
	return {
		Level = 1,
		Xp = 0,
		Gems = 0,
		EquippedItem = "Starter",
		
		OwnedItems = {},
		
	}
end







local function loadData(p)
	local data
	local Success, Error = pcall(function() data = PlayerData:GetAsync("data:"..p.UserId) end)
	
	if not Success then
		warn("Could not get data for "..p.Name)
		return
	end
	
	if not data then
		data = newData()
	end
	
	ServerData[p] = data
	
	
	----------load data---------

	
	
	for i, v in pairs(p.OwnedItems:GetChildren()) do
	
		
	
		if v.ClassName == "BoolValue" then
			
			
			
				if data.OwnedItems[v.Name] then 
					
						---------if user has table
						
						p.OwnedItems[v.Name].Value = data.OwnedItems[v.Name]
						print("Owns "..v.Name)
						print(data.OwnedItems[v.Name])
							
							
				else if not data.OwnedItems[v.Name] then------------they re-generate each time no matter what 
							
					--------if dont have item on table generate it 
						
					table.insert(data.OwnedItems, v.Name)
					data.OwnedItems[v.Name] = false
					
					print("Generated Item  "..v.Name)
					print(data.OwnedItems[v.Name])
					
				
				end


			end
		
		
		
		else
			
			p.OwnedItems.EquippedItem.Value = data.EquippedItem
			
		end
		
		
		
	end 
		
		
		
		

	
	
	
	------------------------------------
	
end

local function saveData(p)
	if not SavingFor[p] then
		SavingFor[p] = true
		
		if ServerData[p] then
			
			
			
			-------save data ------------------------------
			


			
			for i, v in pairs(p.OwnedItems:GetChildren()) do 
				
				 
				
				if v.ClassName == "BoolValue" then
					
					
					ServerData[p].OwnedItems[v.Name] = p.OwnedItems[v.Name].Value
					print("Owns "..v.Name)
					print(ServerData[p].OwnedItems[v.Name])


				else
					
					ServerData[p].EquippedItem = p.OwnedItems.EquippedItem.Value
				end
				
			end
			
			-----------------------------------------------
			
			
			
			
			local Success, Error = pcall(function() PlayerData:SetAsync("data:"..p.UserId, ServerData[p]) end)
			
			if not Success then
				warn("Data wasn't saved for "..p.Name..".")
				return
			end
			
			
			
			
			ServerData[p] = nil
		end
		
		SavingFor[p] = nil
	end
end


Players.PlayerAdded:connect(function(p)
	loadData(p)
end)

Players.PlayerRemoving:connect(function(p)
	saveData(p)
end)

game:BindToClose(function()
	for i,p in pairs(Players:GetChildren()) do
		saveData(p)
	end
end)


You can remove table.insert(data.OwnedItems, v.Name) as it is useless in your code and just fills up the table without any use.
The reason why you are regenerating the data is because your if data.OwnedItems[v.Name] then checks if exists in the table, and if it’s a boolean, it also checks if it is true. Replace it with if data.OwnedItems[v.Name] ~= nil then, this checks if the data actually exists, you can also remove the if not data.OwnedItems[v.Name] then after the else.