Data isn't saving, Why?

I am trying to save 3 stats but they aren’t saving? A reason would be appreciated!
Script:

local SAS = game:GetService("DataStoreService"):GetDataStore("SavingAllStats")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	local Deaths = Instance.new("IntValue", leaderstats)
	Deaths.Name = "Deaths"
	Deaths.Value = 0
	local Kills = Instance.new("IntValue", leaderstats)
	Kills.Name = "Kills"
	Kills.Value = 0
	local Panies = Instance.new("IntValue", leaderstats)
	Panies.Name = "Panies"
	Panies.Value = 0
	local DKSaved = nil
	pcall(function()
		DKSaved = SAS:GetAsync(player.UserId)
	end)
	if DKSaved ~= nil then
		Deaths.Value = DKSaved
		Kills.Value = DKSaved
		Panies.Value = DKSaved
		print("Data Saved!")
	else
		Deaths.Value = 0
		DKSaved.Value = 0
		Kills.Value = 0
		print("New Player")
	end
	player.CharacterAdded:Connect(function(char)
		local Human = char:WaitForChild("Humanoid")
		Human.Died:Connect(function()
			player.leaderstats.Deaths.Value += 1
			local Tag = Human:FindFirstChild("creator")
			local Killer = Tag.Value
			if Tag and Killer then
				Killer.leaderstats.Kills.Value += 1
			end
		end)
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local UserId = player.UserId
	local leaderstats = player:WaitForChild("leaderstats")
	SAS:SetAsync(UserId, leaderstats:WaitForChild("Deaths").Value)
	SAS:SetAsync(UserId, leaderstats:WaitForChild("Kills").Value)
	SAS:SetAsync(UserId, leaderstats:WaitForChild("Panies").Value)
	print("Data Saved!")
end)

3 Likes

First, instead of calling SetAsync` 3 times, insert your stats into a table. And save that table into the data store.

Second,

Does this printing print out?

Or this one

Yeah they are both printing, which surprises me

It’s just that everytime i join i find my 3 stats at 300

Also I am quite bad at using tables in data stores, since it’s my first time trying to save stuff

why are you getting the value? if there is no value in a humanoid

why get value if it has no value. Its a variable

set this to

DKSaved = nil

That was a mistake, Thanks for spotting it!

2 Likes

I had to get help to do that, Since I didn’t know how to get the killer

Does the code have anymore errors

And do have atleast a person helping you to test it.

It is not recommended to use Instance.new with parent argument PSA: Don't use Instance.new() with parent argument

And you should wrap SetAsync in a pcall in case it throws an error.

local SAS = game:GetService("DataStoreService"):GetDataStore("SavingAllStats")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	local Deaths = Instance.new("IntValue")
	Deaths.Name = "Deaths"
	local Kills = Instance.new("IntValue")
	Kills.Name = "Kills"
	local Panies = Instance.new("IntValue")
	Panies.Name = "Panies"
	Deaths.Parent = leaderstats
	Kills.Parent = leaderstats
	Panies.Parent = leaderstats
	leaderstats.Parent = player
	local DKSaved = nil
	local success, err = pcall(function()
		DKSaved = SAS:GetAsync(player.UserId)
	end)
	if success and DKSaved ~= nil then
		Deaths.Value = DKSaved.Deaths
		Kills.Value = DKSaved.Kills
		Panies.Value = DKSaved.Panies
		print("Data loaded!")
	elseif not success then
		warn(err)
	else
		print("New Player")
	end
	player.CharacterAdded:Connect(function(char)
		local Human = char:WaitForChild("Humanoid")
		Human.Died:Connect(function()
			player.leaderstats.Deaths.Value += 1
			local Tag = Human:FindFirstChild("creator")
			local Killer = Tag.Value
			if Tag and Killer then
				Killer.leaderstats.Kills.Value += 1
			end
		end)
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	local UserId = player.UserId
	local leaderstats = player.leaderstats
	local success, err = pcall(function() 
		SAS:SetAsync(UserId, {
			Deaths = leaderstats.Deaths.Value, 
			Kills = leaderstats.Kills.Value, 
			Panies = leaderstats.Panies.Value
		})
	end)
	if not success then
		warn(err)
	end
end)

Surprisingly non does it have, which is strange on why it isn’t saving

Your talking about makng the stats?

sometimes data wont save in studio. do you have the security thingi in settings turned on? any errs?

Yes, that’s exactly what I’m talking about.

Mate, I learnt it that way since i started programming

no errors, I will try testing it in game and inform u

1 Like

you should remove the “WaitForChild” because the player is already in the game why need to add it in