local M=game:GetService("\068\097\116\097\083\116\111\114\101\083\101\114\118\105\099\101")local B=M:GetDataStore("\072\088\068\083")local function A(M)local A={M.leaderstats.Points.Value;M.leaderstats.Gems.Value}local d,Q=pcall(function()B:SetAsync(M.UserId,A)end)if d then print("\068\097\116\097 \104\097\115 \098\101\101\110 \115\097\118\101\100\033")else print("\068\097\116\097 \104\097\115\110\'\116 \098\101\101\110 \115\097\118\101\100\033")warn(Q)end end game.Players.PlayerAdded:Connect(function(M)local A=Instance.new("\070\111\108\100\101\114")A.Name="\108\101\097\100\101\114\115\116\097\116\115"A.Parent=M local d=Instance.new("\073\110\116\086\097\108\117\101")d.Name="\080\111\105\110\116\115"d.Parent=A local Q=Instance.new("\073\110\116\086\097\108\117\101")Q.Name="\071\101\109\115"Q.Parent=A local i local u,L=pcall(function()i=B:GetAsync(M.UserId)end)if u and i then d.Value=i[1]Q.Value=i[2]else print("\084\104\101 \112\108\097\121\101\114 \104\097\115 \110\111 \100\097\116\097\033")end end)game.Players.PlayerRemoving:Connect(function(M)local B,d=pcall(function()A(M)end)if B then print("\068\097\116\097 \104\097\115 \098\101\101\110 \115\097\118\101\100")else print("\068\097\116\097 \104\097\115 \110\111\116 \098\101\101\110 \115\097\118\101\100\033")end end)game:BindToClose(function()for M,B in pairs(game.Players:GetPlayers())do local d,Q=pcall(function()A(B)end)if d then print("\068\097\116\097 \104\097\115 \098\101\101\110 \115\097\118\101\100")else print("\068\097\116\097 \104\097\115 \110\111\116 \098\101\101\110 \115\097\118\101\100\033")end end end)
-- // Assigning variables //
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("HXDS") -- This can be changed to whatever you want

local function saveData(player) -- The functions that saves data
	local tableToSave = {
		player.leaderstats.Points.Value, -- First value from the table
		player.leaderstats.Gems.Value, -- Second value from the table
	}

	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
	end)

	if success then -- If the data has been saved
		print("Data has been saved!")
	else -- Else if the save failed
		print("Data hasn't been saved!")
		warn(err)
	end
end

game.Players.PlayerAdded:Connect(function(player) -- When a player joins the game
	-- // Assigning player stats //
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Points = Instance.new("IntValue")
	Points.Name = "Points"
	Points.Parent = leaderstats

	local Gems = Instance.new("IntValue")
	Gems.Name = "Gems"
	Gems.Parent = leaderstats

	local data -- We will define the data here so we can use it later, this data is the table we saved
	local success, err = pcall(function()
		data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore
	end)

	if success and data then -- If there were no errors and player loaded the data
		Points.Value = data[1] -- Set the money to the first value of the table (data)
		Gems.Value = data[2] -- Set the coins to the second value of the table (data)
	else -- The player didn't load in the data, and probably is a new player
		print("The player has no data!") -- The default will be set to 0
	end
end)

game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
	local success, err = pcall(function()
		saveData(player) -- Save the data
	end)

	if success then
		print("Data has been saved")
	else
		print("Data has not been saved!")
	end
end)

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
		local success, err = pcall(function()
			saveData(player) -- Save the data
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end
	end
end)
