So basically i am having trouble trying to understand how datastorage works, i’ve followed a tutorial on youtube to create my own script but it didn’t work even though there where no erros in the output and i even received the “Data Saved” on the output.
Resume of my problem: I leave the game, get the “data saved” print on output, but when i rejoin the game all my stats have been reseted
(I already published the game, tested it and it still didn’t save data)
Script:
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player) -- WHEN PLAYER JOINS --
local Folder = Instance.new("Folder") -- CURRENT STATS --
Folder.Name = "Stats"
Folder.Parent = player
local Gold = Instance.new("IntValue")
Gold.Name = "Gold"
Gold.Value = 0
Gold.Parent = Folder
local Silver = Instance.new("IntValue")
Silver.Name = "Silver"
Silver.Value = 0
Silver.Parent = Folder
local Bronze = Instance.new("IntValue")
Bronze.Name = "Bronze"
Bronze.Value = 0
Bronze.Parent = Folder
local Rushies = Instance.new("IntValue")
Rushies.Name = "Rushies"
Rushies.Value = 0
Rushies.Parent = Folder
local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Value = 1
Level.Parent = Folder
local Exp = Instance.new("IntValue")
Exp.Name = "Exp"
Exp.Value = 0
Exp.Parent = Folder
local MaxExp = Instance.new("IntValue")
MaxExp.Name = "MaxExp"
MaxExp.Value = Level.Value*100
MaxExp.Parent = Folder
local Leaderboard = Instance.new("Folder") -- LEADERSTATS STUFF --
Leaderboard.Name = "leaderstats"
Leaderboard.Parent = player
local LeaderboardWins = Instance.new("IntValue")
LeaderboardWins.Name = "Wins"
LeaderboardWins.Value = Gold.Value
LeaderboardWins.Parent = Leaderboard
local LeaderboardLv = Instance.new("IntValue")
LeaderboardLv.Name = "Lv"
LeaderboardLv.Value = Level.Value
LeaderboardLv.Parent = Leaderboard
local function UpdateLeaderstats()
LeaderboardWins.Value = Gold.Value
LeaderboardLv.Value = Level.Value
end
Level.Changed:Connect(UpdateLeaderstats)
Gold.Changed:Connect(UpdateLeaderstats)
Exp.Changed:Connect(function()
if Exp.Value >= MaxExp.Value then
Level.Value = Level.Value + 1
Exp.Value = 0
MaxExp.Value = MaxExp.Value+50
end
local success, errormessage = pcall(function()
local data = myDataStore:UpdateAsync(player.UserId)
if data then
data.goldTrophy = player.Stats.Gold.Value
data.silverTrophy = player.Stats.Silver.Value
data.bronzeTrophy = player.Stats.Bronze.Value
data.rushies = player.Stats.Rushies.Value
data.level = player.Stats.Level.Value
data.exp = player.Stats.Exp.Value
data.maxExp = player.Stats.MaxExp.Value
else
print("There was an error while saving data!")
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player) -- WHEN PLAYER LEAVES --
local success, errormessage = pcall (function()
local data = {}
data.goldTrophy = player.Stats.Gold.Value
data.silverTrophy = player.Stats.Silver.Value
data.bronzeTrophy = player.Stats.Bronze.Value
data.rushies = player.Stats.Rushies.Value
data.level = player.Stats.Level.Value
data.exp = player.Stats.Exp.Value
myDataStore:SetAsync(player.UserId, data)
end)
if success then
print("Data Saved!")
else
print("There was an error while saving data!")
warn(errormessage)
end
end)
end)
Not the guy you’re replying to, but a highly suggest a module called DataStore2.
If you use the code you’ve written, you’re going to have data loss.
Because of some pretty major glaring flaws like using :SetAsync (Stop using SetAsync() to save player data), and updating data too fast (ie. updating after .Changed will make you hit the data rate limit extremely fast).
Ironically i have just discovered DataStore2 before you said so xD, i am currently watching AlvinBlox video and trying to understand how it should work, hope i can make it work properly
Just a question about what you’ve said related to “hitting the data rate limit”.
The only times i use the .Changed is to visually update the leaderstats based on the current Stats of the player and when the Exp reaches the MaxExp, i believe this has nothing to do with the datastorage since it only saves when the player leaves, or i am mistaken?
I used a datastore I scripted a while back for my game. It’s more simple and it’s not as complicated as yours and it’s easier to add more values to save if you add more stats in the future. The thing wrong with yours was that you were searching for the value inside the player when the values were already set above the code. You only needed to search for the values when you did the player leaving function. Also, you tried to update the async before anything was set.
I haven’t used DataStore2 before and DataStore2 isn’t implemented by ROBLOX. It’s a module made from a developer. AlvinBlox has a tutorial explaining how to use it. Make a new script and follow the tutorial and then transfer your values over to the DataStore2 script.