Trying to save class stat values not working

Hello,
I’m trying to make stats for every class in my game, and it records stats like wins and kills. I tried to make it save all of it with a for loop that runs through every folder in the player’s Class Stats folder and inserts their Win and Kill data in a table that will be saved.

The problem is, these values don’t save in the table. When I set a kill count for a random class to a random number like 30, I leave studio (API services is enabled) and rejoin to see the value go back to 0.

It’s really confusing me and I don’t know how to solve this problem.

local DS = game:GetService("DataStoreService"):GetDataStore("PlayerStats")

game.Players.PlayerAdded:Connect(function(p)
    local fo = Instance.new("Folder",p)
	fo.Name = "ClassStats"
	
	for i, v in pairs(game.ServerStorage.Classes:GetChildren()) do
		local f = Instance.new("Folder", fo)
		f.Name = v.Name
		
		local w = Instance.new("IntValue",f)
		w.Name = "Wins"
		
		local k = Instance.new("IntValue",f)
		k.Name = "Kills"
	end
	
	wait(1)
	local plrKey = "stats_"..p.UserId

	local GetSaved = DS:GetAsync(plrKey)
	if GetSaved then
		for i, v in pairs (p.ClassStats:GetChildren()) do
			
			v.Wins.Value = GetSaved[#GetSaved + 1]
			v.Kills.Value = GetSaved[#GetSaved + 1]
		end
		
	else 
		local NumbersForSaving = {}
		
		for i, v in pairs (p.ClassStats:GetChildren()) do
			
			table.insert(NumbersForSaving, v.Wins.Value)
			table.insert(NumbersForSaving, v.Kills.Value)
		end
		
		DS:GetAsync(plrKey, NumbersForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(p)
	local Table = {}
	
	for i, v in pairs (p.ClassStats:GetChildren()) do
		table.insert(Table, v.Wins.Value)
		table.insert(Table, v.Kills.Value)
	end
	
	DS:SetAsync("stats_"..p.UserId, Table)
end)
1 Like