From primarily looking at the above code that should fix the errors, but i would like to recommend some improvements in general, firstly i would suggest using pcalls and using UpdateAsync (there are a couple threads that go into detail why and which one you should use), but for now here is my example of using Update Async wrapped in a pcall:
local Success, Error = pcall(function() --- returns success and, err if there is one
DataStore:UpdateAsync(key, function(prev) -- prev is previously saved data
return items ---saves new data
end)
end)
i’m not sure what you are asking but if you are talking about the pcall, the point of it (in this case) is for in the unlikely event that data isn’t saved/loaded due to a roblox-related issue. Try using without it first, to ensure that the rest of your script isn’t faulty
it didnt work, I’m almost crying…
i played on the game then i added xp, leveled up to 2 and when i re opened the game my status was reseted or something like
Well, this is where you need to start debugging , to find out where the problem is occuring. Try adding in some prints where your Player Removing and Player Added events are:
local storeditems = DataStore:GetAsync(key)
print(storeditems)
print(items)
DataStore:SetAsync(key, items)
From what i can see in your code, everything should work
Also are there any errors in the output that might help you with this debugging process?
local storeditems = DataStore:GetAsync(key)
print(storeditems)
if storeditems then
Level.Value = storeditems[1]
Rank.Value = storeditems[2]
XP.Value = storeditems[3]
Coins.Value = storeditems[4]
FC.Value = storeditems[5]
else
local items = {Level.Value, Rank.Value, XP.Value, FC.Value, Coins.Value} --Change Points or Wins if you changed line 10 or 14
print(items)
DataStore:SetAsync(key, items)
end
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Leaderstats") -- Change this with a random name.
game.Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = Player
local XP = Instance.new("StringValue")
XP.Name = "XP" -- Change this with your XP name.
XP.Value = DataStore:GetAsync(Player.UserId) or 0
XP.Parent = Leaderstats
local Level = Instance.new("StringValue")
Level.Name = "Level" -- Change this with your XP name.
Level.Value = DataStore:GetAsync(Player.UserId) or 1
Level.Parent = Leaderstats
local Fc = Instance.new("StringValue")
Fc.Name = "FC" -- Change this with your XP name.
Fc.Value = DataStore:GetAsync(Player.UserId) or 100
Fc.Parent = Leaderstats
local Mny = Instance.new("StringValue")
Mny.Name = "Coins" -- Change this with your XP name.
Mny.Value = DataStore:GetAsync(Player.UserId) or 100
Mny.Parent = Leaderstats
while wait() do
wait(.01)
if XP.Value >= (100 * (Level.Value + 1)) then
Level.Value = Level.Value + 1
XP.Value = 0
game.ReplicatedStorage.LevelUpGui:FireClient(Player)
end
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, {
["XP"] = Player.leaderstats.XP.Value;
["Level"] = Player.leaderstats.Level.Value;
["FC"] = Player.leaderstats.FC.Value;
["Coins"] = Player.leaderstats.Coins.Value;
-- ["Level"] = Player.leaderstats.Level.Value;
-- ["Level"] = Player.leaderstats.Level.Value;
-- ["Level"] = Player.leaderstats.Level.Value;
-- ["Level"] = Player.leaderstats.Level.Value;
})
end)
i’ve played around with you script and found out that the SetAsync is not being fully executed (at least for me) in studio, possibly due to the server shutting down too quickly (which game:BindToClose can “fix”)
Anyways Here is my edited code:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataSaved = {}
Players.PlayerAdded:Connect(function(Plr)
local leaderstats = Instance.new("IntValue")
leaderstats.Name = "leaderstats"
leaderstats.Parent = Plr
local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Value = 1
Level.Parent = leaderstats
local XP = Instance.new("IntValue")
XP.Name = "XP"
XP.Value = 0
XP.Parent = leaderstats
local Rank = Instance.new("StringValue")
Rank.Name = "Rank"
Rank.Value = "Genin"
Rank.Parent = leaderstats
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = 10
Coins.Parent = leaderstats
local FC = Instance.new("IntValue")
FC.Name = "FC"
FC.Value = 100
FC.Parent = leaderstats
local key = "user-"..Plr.userId
local storeditems
local items = {Level.Value, Rank.Value, XP.Value,Coins.Value, FC.Value}
local success, err = pcall(function()
storeditems = DataStore:GetAsync(key) or items
end)
if storeditems then
Level.Value = storeditems[1]
Rank.Value = storeditems[2]
XP.Value = storeditems[3]
Coins.Value = storeditems[4]
FC.Value = storeditems[5]
end
while wait() do
wait(.01)
if XP.Value >= (100 * (Level.Value + 1)) then
Level.Value = Level.Value + 1
XP.Value = 0
game.ReplicatedStorage.LevelUpGui:FireClient(Player)
end
end
end)
game:BindToClose(function()
for _, plr in ipairs(Players:GetPlayers()) do
if not DataSaved[plr] then
local items = {plr.leaderstats.Level.Value, plr.leaderstats.Rank.Value, plr.leaderstats.XP.Value, plr.leaderstats.Coins.Value, plr.leaderstats.FC.Value}
local key = "user-"..plr.userId
local success, err = pcall(function()
DataStore:UpdateAsync(key, function(prev)
return items
end)
end)
end
end
end)
Players.PlayerRemoving:connect(function(player)
local items = {player.leaderstats.Level.Value, player.leaderstats.Rank.Value, player.leaderstats.XP.Value, player.leaderstats.Coins.Value, player.leaderstats.FC.Value} --Change Points or Wins if you changed line 10 or 14
local key = "user-"..player.userId
local success, err = pcall(function()
DataStore:UpdateAsync(key, function(prev)
return items
end)
end)
if success then
DataSaved[player] = true
end
end)
Also since i am guessing you want to use this for your game, it’s probably a good idea to add some datastore “retries”. there are also some good articles on the wiki and the devforoum that are useful: