Try testing it in like the actual game instead of studio!
I’ve tried this and tested it out but it’s straightforward.
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("MoneyStats") -- Change this with a different name.
game.Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = "leaderstats"
local XP = Instance.new("IntValue", Leaderstats)
XP.Name = "XP"
XP.Value = 0
local EvolutionPoints = Instance.new("IntValue", Leaderstats)
EvolutionPoints.Name = "Evolution Points"
EvolutionPoints.Value = 0
local Level = Instance.new("IntValue", Leaderstats)
Level.Name = "Level"
Level.Value = 0
local Data = DataStore:GetAsync(Player.UserId)
if Data then
XP.Value = Data.XP -- Change to "XP" to ur currency
EvolutionPoints.Value = Data.EvolutionPoints -- Change "EvolutionPoints" with your currency.
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, {
["XP"] = Player.leaderstats.XP.Value;
["Level"] = Player.leaderstats.XP.Value;-- Change "Level" with your currency.
["Evolution Points"] = Player.leaderstats.EvolutionPoints.Value; -- Change "EvolutionPoints" with your currency.
})
end)
Seems to me your not using “BindToClose”, whenever I use datastores. I always put the bindtoclose function right after the game.Players.PlayerRemoving function.
Try this:
local DSS = game:GetService("DataStoreService")
local MDS = DSS:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local EP = Instance.new("IntValue")
EP.Name = "Evolution Points"
EP.Parent = leaderstats
local XP = Instance.new("IntValue")
XP.Name = "XP"
XP.Parent = leaderstats
local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Parent = leaderstats
local data
local data2
local data3
local success, errormessage = pcall(function()
data = MDS:GetAsync(plr.UserId.."-Evolution Points")
data2 = MDS:GetAsync(plr.UserId.."-XP")
data3 = MDS:GetAsync(plr.UserId.."-Level")
end)
if success then
EP.Value = data
XP.Value = data2
Level.Value = data3
else
print("Failed!")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success, errormessage = pcall(function()
MDS:SetAsync("id_"..plr.UserId, plr.leaderstats["Evolution Points"].Value, plr.leaderstats.XP.Value, plr.leaderstats.Level.Value)
end)
if success then
print("Success!")
else
print("Failed!")
warn(errormessage)
end
end)
game:BindToClose(function()
wait(1)
end)
Read this, if you want to. It helped me realize the same thing: DataStores - Beginners to Advanced
The reason your script wasn’t working was because your “SetAsync()” call was made with 4 values passed as arguments to it when it only expects 2 arguments, because of this the additional 2 values (which were 2 of the leaderstat’s stats) were discarded and not saved. Instead of attempting to save/load each stat’s value individually you can instead collect all of the values into a single table value (array) and save/load that to/from the DataStore.
local Players = game:GetService("Players")
local DataStores = game:GetService("DataStoreService")
local DataStore = DataStores:GetDataStore("DataStore")
local ProtectedCall = pcall
Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = Player
local EP = Instance.new("IntValue")
EP.Name = "Evolution Points"
EP.Parent = Leaderstats
local XP = Instance.new("IntValue")
XP.Name = "XP"
XP.Parent = Leaderstats
local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Parent = Leaderstats
local Success, Result = ProtectedCall(function()
return DataStore:GetAsync(Player.UserId.."_Stats")
end)
if Success then
if Result then
if type(Result) == "table" then
EP.Value = Result[1]
XP.Value = Result[2]
Level.Value = Result[3]
end
end
else
warn(Result)
end
end)
Players.PlayerRemoving:Connect(function(Player)
local Success, Result = ProtectedCall(function()
return DataStore:SetAsync(Player.UserId.."_Stats", {Player.leaderstats["Evolution Points"].Value, Player.leaderstats.XP.Value, Player.leaderstats.Level.Value})
end)
if Success then
print(Result)
else
warn(Result)
end
end)
game:BindToClose(function()
for _, Player in ipairs(Players:GetPlayers()) do
local Success, Result = ProtectedCall(function()
return DataStore:SetAsync(Player.UserId.."_Stats", {Player.leaderstats["Evolution Points"].Value, Player.leaderstats.XP.Value, Player.leaderstats.Level.Value})
end)
if Success then
print(Result)
else
warn(Result)
end
end
end)
I did not know that (always learning new things). Thanks! I’ll try it.