Problem with datastores

So for the first time I decided to take my shot at datastores, so recently I created a leveling and money system from a tutorial and i wanted to save that and saving the money worked fine but the level didn’t
(The money is in a folder called leaderstats but the level is just a value inside your local player idk if that has something to do with it)
This is the script but most of it is the level and money script so it might be hard to find all the data stuff:

local DataStoreService = game:GetService(“DataStoreService”)
local myDataStore = DataStoreService:GetDataStore(“MyDataStore”)

game.Players.PlayerAdded:Connect(function(plr)

local leaderstats = Instance.new("Folder",plr)
leaderstats.Name = "leaderstats"

local money = Instance.new("NumberValue",leaderstats)
money.Name = "Money"

local playerUserId = "Player_"..plr.UserId

local level = Instance.new("IntValue",plr)
level.Name = "Level"
level.Value = 1

local exp = Instance.new("IntValue",level)
exp.Name = "Current"
exp.Value = 0

local maxExp = Instance.new("IntValue",level)
maxExp.Name = "Max"
maxExp.Value = 100

local data
local success, errormessage = pcall(function()
	data = myDataStore:GetAsync(playerUserId)
end)

if success then
	if data then
		money.Value = data.Money
		level.Value = data.Level
		end
end

exp.Changed:Connect(function(val)
	if exp.Value >= maxExp.Value then
		level.Value = level.Value +1
		exp.Value = exp.Value - maxExp.Value
		maxExp.Value = maxExp.Value *1.25
	end
end)


level.Changed:Connect(function()
	money.Value = money.Value + level.Value *20
end)

end)

game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = “Player_”…player.UserId

local data = {
	money = player.leaderstats.Money.Value;
	level = player.Level.Value
}
local success, errormessage = pcall(function()
	myDataStore:SetAsync(playerUserId, data)
end)

if success then
	print("Data successfully saved")
else
	print("There was an error")
	warn(errormessage)
end

end)

workspace:WaitForChild(“ExpGiver”).ClickDetector.MouseClick:Connect(function(player)
player.Level.Current.Value = player.Level.Current.Value +25
end)

workspace:WaitForChild(“SuperExpGiver”).ClickDetector.MouseClick:Connect(function(player)
if player.Level.Value >= 10 then
player.Level.Current.Value = player.Level.Current.Value +200
end
end)

Use DataModel:BindToClose() function, because the server closes and all scripts stop when all players leaves the game.

Let me know if this works correctly :slight_smile:

local setAsyncQueue = 0
game.Players.PlayerRemoving:Connect(function(player)
   local playerUserId = "Player_" .. player.UserId

   local data = {
   	money = player.leaderstats.Money.Value;
   	level = player.Level.Value
   }
   
   setAsyncQueue += 1
   local success, errormessage = pcall(function()
   	myDataStore:SetAsync(playerUserId, data)
   end)

   setAsyncQueue -= 1
   if success then
   	print("Data successfully saved")
   else
   	print("There was an error")
   	warn(errormessage)
   end
end)

game:BindToClose(function()
   repeat
   	while setAsyncQueue > 0 do -- Waits until there are no GlobalDataStore:SetAsync requests
   		wait()
   	end
   wait(--[[Add here an amount of seconds]]) -- Waits a bit more time if there was any request that wasn't catched by the previous function
   until setAsyncQueue == 0	
end)