I'm trying to save multiple values in a data store but only the Money saves and the XP doesn't, how may I fix this?

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = leaderstats
	
	local XP = Instance.new("IntValue")
	XP.Name = "XP"
	XP.Parent = leaderstats
	
	local data
	local data2
	local success, errormessage = pcall(function()
		data = myDataStore:GetAsync(player.UserId.."-Money")
		data2 = myDataStore:GetAsync(player.UserId.."-XP")
	end)
	
	if success then
		money.Value = data
	else
		print("There was an error whilst getting your data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-Money",player.leaderstats.Money.Value)
	        myDataStore:SetAsync(player.UserId.."-XP",player.leaderstats.XP.Value)
	end)
	if success then
		print("Data Succesfully Loaded")
	else
		print("Error saving Data")
		warn(errormessage)
	end
end)

1 comment