Stat datastore not saving

Hello, I have a KOs script that’s supposed to save to a datastore. I’ve been trying to get this to work for a while and I just can’t tell what’s wrong with it, getting kills works but once you leave and rejoin they don’t save. Here’s the script:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("KillsDataStore")

game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"

	local Kills= Instance.new("IntValue", Leaderstats)
	Kills.Name = "Kills"
	Kills.Value = 0
				
	local Data = DataStore:GetAsync(Player.UserId)
	if Data then
		Kills.Value = Data.Kills
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, {

		["Kills"] = Player.leaderstats.Kills.Value;
	})
end)
local Players = game.Players

local Template = Instance.new 'BoolValue'
Players.PlayerAdded:connect(function(Player)
	local Stats = Template:Clone()
	Stats.Parent = Player
	Player.CharacterAdded:connect(function(Character)
		local Humanoid = Character:FindFirstChild "Humanoid"
		if Humanoid then
			Humanoid.Died:connect(function()
				for i, Child in pairs(Humanoid:GetChildren()) do
					if Child:IsA('ObjectValue') and Child.Value and Child.Value:IsA('Player') then
						local Killer = Child.Value
						if Killer:FindFirstChild 'leaderstats' and Killer.leaderstats:FindFirstChild "Kills" then
							local Kills = Killer.leaderstats.Kills
							Kills.Value = Kills.Value + 1
						end
						return
					end
				end
			end)
		end
		end)
	end)

Can anybody tell me what’s wrong with it ?

  • This is inside of serverscriptservice

Have you enabled studio access to API services? And try following this, it might help you: Introduction to Saving Data