Datastore not saving?

  1. I want to be able to save my data in a datastore, without it failing.

  2. The data keeps failing to save, and resets.

  3. I have tried changing the datastore key, but nothing worked.

Script:

local Data = game:GetService("DataStoreService"):GetDataStore("GlobalDataStoreTesting_4")

local function PlayerAdded(Player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"

	local Stat1 = Instance.new("NumberValue")
	Stat1.Name = "Time"
	local Stat2 = Instance.new("NumberValue")
	Stat2.Name = "Best Time"
	local Stat3 = Instance.new("NumberValue")
	Stat3.Name = "Kills"
	local Stat4 = Instance.new("BoolValue")
	Stat4.Name = "InSafeZone"
	local Stat5 = Instance.new("BoolValue")
	Stat5.Name = "FirstTimePlay"
	local Stat6 = Instance.new("NumberValue")
	Stat6.Name = "Level"
	local Stat7 = Instance.new("NumberValue")
	Stat7.Name = "Exp"
	local Stat8 = Instance.new("NumberValue")
	Stat8.Name = "MaxExp"

	Stat1.Parent = leaderstats
	Stat2.Parent = leaderstats
	Stat3.Parent = leaderstats
	Stat4.Parent = Player
	Stat5.Parent = Player
	Stat6.Parent = leaderstats
	Stat7.Parent = Player
	Stat8.Parent = Player
	leaderstats.Parent = Player

	local playerUserId = "Player_"..Player.UserId

	local data
	local success, err = pcall(function()
		data = Data:GetAsync(playerUserId)
	end)

	Stat1.Value = data and data[1] or 0
	Stat2.Value = data and data[2] or 0
	Stat3.Value = data and data[3] or 0
	Stat6.Value = data and data[4] or 1
	Stat7.Value = data and data[5] or 0
	Stat4.Value = true
	
	local function p()
	while wait(1) do
		if Stat4.Value == false then
			Stat1.Value += 1
		end
		if Stat1.Value > Stat2.Value then
			Stat2.Value = Stat1.Value
		end
		end
	end	
	spawn(p)
	
	while wait(0.1) do
		Stat8.Value = Stat6.Value * 375
		if Stat7.Value >= Stat8.Value then
			Stat7.Value = 0
			Stat6.Value += 1
		end
	end
end

game.Players.PlayerAdded:Connect(PlayerAdded)

game.Players.PlayerRemoving:Connect(function(Player)
	local playerUserId = "Player_"..Player.UserId
	local Folder = Player:FindFirstChild("leaderstats")

	local success, err = pcall(function()
		Data:SetAsync(playerUserId,{
			Folder.Time.Value,
			Folder["Best Time"].Value,
			Folder.Kills.Value,
			Folder.Level.Value,
			Folder.Exp.Value
		})
	end)
end)

game:BindToClose(function()
	wait(3)
end)

If anyone has an answer, then please tell me.

I found the error. I didn’t save the stuff inside the player. I only saved the stuff inside the leaderstats folder, and tried to find something in leaderstats, that was actually inside the player.