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)