Good day, Scripting Helpers. I'm a bit unaware of all the new Roblox features so until recently, I did not know of Data Store saving so I used Data Persistence instead. However, because of Data Persistence issues, I decided on using Data Store instead. I watched a tutorial about how to apply Data Store saving on a stats/leaderstats script and tried to use it for my own stats script. For some reason though, my stats don't save at all. I would really appreciate if someone could explain to me what did I do wrong.
Stats script
local DataStore = game:GetService("DataStoreService"):GetDataStore("PrimpyGameData") -- The first two functions are most likely irrelevant to my issue. function onXPChanged(player, XP, level) if XP.Value>=level.Value * 40 then XP.Value = XP.Value - level.Value * 40 level.Value = level.Value + 1 hp.Value = true -- I removed this value (hp) from THIS script because it isn't relevant. It exists though. end end function onLevelUp(player, XP, level) if level.Value>=100 then XP.Value = 0 level.Value = 100 end end function onPlayerEntered(newPlayer) local stats = Instance.new("IntValue", newPlayer) stats.Name = "stats" local xp = Instance.new("IntValue", stats) xp.Name = "XP" xp.Value = 0 local level = Instance.new("IntValue", stats) level.Name = "Level" level.Value = 1 -- Lots of other values similar to level and xp go here. I tested only for level and xp though. local key = "player-"..newPlayer.userId local savedValues = DataStore:GetAsync(key) if savedValues then xp.Value = savedValues[1] level.Value = savedValues[2] -- A bit off-topic but if I add more stats/values, do I just increase the number of -- savedValues? Like, savedValues[3], savedValues[4] and so on? else local valuesToSave = {xp.Value, level.Value} DataStore:SetAsync(key, valuesToSave) end -- Again, below is most likely irrelevant. xp.Changed:connect(function() onXPChanged(newPlayer, xp, level) end) level.Changed:connect(function() onLevelUp(newPlayer, xp, level) end) newPlayer.Changed:connect(function (property) if (property == "Character") then onPlayerRespawned(newPlayer) end end) end game.Players.ChildAdded:connect(onPlayerEntered)
Stats saving on player leaving script
local DataStore = game:GetService("DataStoreService"):GetDataStore("PAStats") game.Players.PlayerRemoving:connect(function(player) local key = "player-"..player.userId local valuesToSave = {player.stats.Level.Value, player.stats.XP.Value} DataStore:SetAsync(key, valuesToSave) end)
Thank you so much for reading, any kind of answers are appreciated. Have a great day '~'
Fixed script Here is the fixed script. I did stats savind and loading in one script. Just paste this to the script in ServerScriptService. Stats script
local DataStore = game:GetService("DataStoreService"):GetDataStore("PrimpyGameData") -- The first two functions are most likely irrelevant to my issue. function onXPChanged(player, XP, level) if XP.Value>=level.Value * 40 then XP.Value = XP.Value - level.Value * 40 level.Value = level.Value + 1 hp.Value = true -- I removed this value (hp) from THIS script because it isn't relevant. It exists though. end end function onLevelUp(player, XP, level) if level.Value>=100 then XP.Value = 0 level.Value = 100 end end function onPlayerEntered(newPlayer) local stats = Instance.new("IntValue", newPlayer) stats.Name = "stats" local xp = Instance.new("IntValue", stats) xp.Name = "XP" xp.Value = 0 local level = Instance.new("IntValue", stats) level.Name = "Level" level.Value = 1 -- Lots of other values similar to level and xp go here. I tested only for level and xp though. local key = "player-"..newPlayer.userId local savedValues = DataStore:GetAsync(key) if savedValues then xp.Value = savedValues[1] level.Value = savedValues[2] -- A bit off-topic but if I add more stats/values, do I just increase the number of -- savedValues? Like, savedValues[3], savedValues[4] and so on? else local valuesToSave = {level.Value, xp.Value} DataStore:SetAsync(key, valuesToSave) end -- Again, below is most likely irrelevant. xp.Changed:connect(function() onXPChanged(newPlayer, xp, level) end) level.Changed:connect(function() onLevelUp(newPlayer, xp, level) end) newPlayer.Changed:connect(function (property) if (property == "Character") then onPlayerRespawned(newPlayer) end end) end game.Players.ChildAdded:connect(onPlayerEntered) --local DataStore = game:GetService("DataStoreService"):GetDataStore("PAStats") <- Here was the error, you didnt save datas in same datastore game.Players.PlayerRemoving:connect(function(player) local key = "player-"..player.userId local valuesToSave = {player.stats.Level.Value, player.stats.XP.Value} DataStore:SetAsync(key, valuesToSave) end)