pls help. it prints "data saved" when i test my game with two players on studio, but when i join the game and reset my character, and then join back my death won't save.
local dataStoreService = game:GetService("DataStoreService")
local myDataStore = dataStoreService:GetDataStore("myDataStore")
wait(5)
game.Players.PlayerAdded:connect(function(player)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local kills = Instance.new("IntValue",leaderstats)
kills.Name = "Kills"
local deaths = Instance.new("IntValue",leaderstats)
deaths.Name = "Deaths"
local data
local success, errormsg = pcall(function()
data = myDataStore:SetAsync(player.UserId,player.leaderstats.Deaths.Value)
end)
if success then
player.leaderstats.Deaths.Value = data
print("data loaded")
else
warn(errormsg)
print("Data not saved")
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success, errormsg = pcall(function()
myDataStore:SetAsync(plr.UserId,plr.leaderstats.Deaths.Value)
end)
if success then
print("Data saved")
else
print("Data not saved")
warn(errormsg)
end
end)
game.Players.PlayerAdded:connect(function(plr)
plr.CharacterAdded:connect(function()
local humanoid = plr.Character:WaitForChild("Humanoid")
local deaths = plr.leaderstats.Deaths
humanoid.Died:connect(function()
deaths.Value = deaths.Value + 1
end)
end)
end)

3 comments
Line 9 should be :GetAsync(player.UserId) instead of :SetAsync(player.UserId), for getting the stats, you don't need to define the value as it's returned as a table. And you can add your characterAdded, death stuff in the first player added under the 2nd to last end.
works now, thank you! also one more question, how would i save multiple values at once? e.g. saving kills and deaths
nvm i read your code i'll try to put it into a table