Data isn't saving, Why?

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

plus you should update the script in the beginning for more understanding

Wdym by update the script at the beginning?

when you first created the post update that

I tried seeing if it works in game but it doesn’t save and I checked console It printed Data saved message with no errors…

hmm, time to spoon feed since nothin i can do :slight_smile:

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.deaths
		Kills.Value = DKSaved.kills
		Panies.Value = DKSaved.panies
		print("Balls :)")
	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 saveData = {
        deaths = player.leaderstats.Deaths.Value,
        kills =  player.leaderstats.Kills.Value,
        panies =  player.leaderstats.Panies.Value
	local leaderstats = player:WaitForChild("leaderstats")
	local saved, err = pcall(function()
                SAS:SetAsync(UserId, saveData)
        end
        if saved then print('data saved') else warn(err) end
         

end)

here ya go mah boi, not the recommended saving method but it works. On the first save in studio it will for sure error, and then after setting the value it will work the second time. Reminder, this only happens in studio. It should work everywhere else just fin

2 Likes

I actually got the answer yesterday from someone, But this is the exact same way on how he helped me, Thank you! I will put it as a solution incase anyone runs into it. Thanks again!

No problem, glad you found the answer. I am sorry for my late reply, as I was out at a party that time