Datastore not saving

I made a datastore for my xp but it doesn’t save the only this that is saving is my level what should i do

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game:GetService("Players").PlayerAdded:Connect(function(plr)
 
	local stats = Instance.new("Folder")
	stats.Name = "stats"
	stats.Parent = plr
	
	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Value = 1
	Level.Parent = plr:WaitForChild("leaderstats")
	
	local currentXP = Instance.new("IntValue")
	currentXP.Name = "currentXP"
	currentXP.Parent = stats
	
	local maxXP = Instance.new("IntValue")
	maxXP.Name = "maxXP"
	maxXP.Value = 100
	maxXP.Parent = stats
	
	local data
	local currentData
	local maxData
	local success, errormessage = pcall(function()
		
		data = myDataStore:GetAsync(plr.UserId.."-Level")
		currentData = myDataStore:GetAsync(plr.UserId.."-curentXP")
		maxData = myDataStore:GetAsync(plr.UserId.."-maxXP")
	end)
	if success then
		Level.Value = data 
		currentXP.Value = currentData
		maxXP.Value = maxData
	else
		warn(errormessage)
		print("There was an error saving data")
	end
end)


game.Players.PlayerRemoving:Connect(function(plr)
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(plr.UserId.."-Level", plr.leaderstats.Level.Value)
		myDataStore:SetAsync(plr.UserId.."-currentXP", plr.stats.currentXP.Value)
		myDataStore:SetAsync(plr.UserId.."-maxXP", plr.stats.maxXP.Value)
	end)
	
	if success then
		print("Player's Data successfully saved")
	else
		print("There was an error saving the data")
		wait(errormessage)
	end
	
	
end)

When i change the value of currentXP and maxXP and left the game it doesn’t give me any prints print(“There was an error saving the data”) nor print(“Player’s Data successfully saved”) is something wrong with my script

Well when you are the last to leave the server the save might not have enough time to run. Add this at the end

game:BindToClose(function()
 for i, player in pairs(game.Players:GetPlayers()) do
  if player then
   player:Kick()
  end
 end

 wait(5) -- This is so the game can save data
end)
3 Likes

I recommend saving everything within one datastore like this, it will also fix your issue
I’ve scripted this very quick tell me if there is any issues with it.

local DatastoreService = game:GetService("DataStoreService")
local Datastore = DatastoreService:GetDataStore("PlayerData")

local CreateLeaderboard = function(Player, Data)
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player
	
	--Make the values
	local Level = Instance.new("NumberValue")
	Level.Name = "Level"
	Level.Value = Data.Level --Get the value from our array
	
	--Do the rest
end

game.Players.PlayerAdded:Connect(function(Player)
	local Success, Data = pcall(function()
		return Datastore:GetAsync(Player.UserId)
	end)
	
	if Success then
		if Data then
			CreateLeaderboard(Player, Data) --Pass saved data
		else
			--New player
			local Data = {
				["Level"] = 0,
				["CurrentExp"] = 0,
				["MaxExp"] = 100
			}
			CreateLeaderboard(Player, Data) --Pass the data we just made for a new player
		end
	else
		--Doesnt work do something else
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	local Data = {
		["Level"] = Player.leaderstats.Level.Value,
		["CurrentExp"] = Player.leaderstats.CurrentExp.Value,
		["MaxExp"] = Player.leaderstats.MaxExp.Value,
	}
	Datastore:SetAsync(Player.UserId, Data)
end)
1 Like

it saved the maxXP but it didn’t saved the currentXP i tried making the wait a little longer by replacing the 5 to 10 so it have more time to save the data but the currentXP didn’t save

You’ve spelt current wrong when using GetAsync()

1 Like

I found out why the currentXP didn’t save because i had a typographical error on the script

I’ve given you a more optimized version of your datastore if you’d like to learn from it or use it.

1 Like