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)
Error 1
Line 9 Should be :GetAsync(), instead of :SetAsync()
:GetAsync() is to get the stored data for the player.
:SetAsync() is to set the new values for the player.
These aren't really errors, but just tips.
For getting the stats, you don't need to define the value as it's returned as a table, so you would have to index the table if you have multiple values.
And you can add your characterAdded, death stuff in the first player added under the 2nd to last end
This is how I would do it, even though I saw an error and a few improvements.
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:GetAsync(player.UserId) end) if success then player.leaderstats.Deaths.Value = data[1] or 0 player.leaderstats.Kills.Value = data[2] or 0 --// Edited print("Data loaded") else warn(errormsg) print("Data not saved") end player.CharacterAdded:connect(function(character) local humanoid = character:WaitForChild("Humanoid") local deaths = player.leaderstats.Deaths humanoid.Died:connect(function() deaths.Value = deaths.Value + 1 end) end) end) game.Players.PlayerRemoving:Connect(function(plr) local dataToSave = { plr.leaderstats.Deaths.Value, plr.leaderstats.Kills.Value --// Edited } local success, errormsg = pcall(function() myDataStore:SetAsync(plr.UserId,dataToSave) end) if success then print("Data saved") else print("Data not saved") warn(errormsg) end end)
Hope this helped and worked! From my experience, DataStores have been janky very recently.