Saving Data of a Player outside of a server

I’m trying to save the data of a player outside the server but I can’t seem to get it to work.

Here’s the code, (the one in comment form is what I added)

local AUTO_SAVE = true			-- Make true to enable auto saving
local TIME_BETWEEN_SAVES = 120	-- In seconds (WARNING): Do not put this lower than 60 seconds
local PRINT_OUTPUT = false		-- Will print saves and loads in the output
local SAFE_SAVE = false			-- Upon server shutdown, holds server open to save all data
---------------------------------

local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local leaderboardData = dataStoreService:GetDataStore("CurrencyGAMING-00000500")

local function Print(message)
	if PRINT_OUTPUT then print(message) end
end

--local Players = game:GetService("Players")
--local cache = {}

--local function getUserIdFromUsername(name)
	--if cache[name] then return cache[name] end
--	local player = Players:FindFirstChild(name)
----	if player then
--		cache[name] = player.UserId
	--	return player.UserId
--	end 
--	local id
--	pcall(function ()
--		id = Players:GetUserIdFromNameAsync(name)
--	end)
--	cache[name] = id
--	return id
--end

--local function updatePlayerDataFromServer(player, bgName, val)
--	local id = getUserIdFromUsername(player)
--	leaderboardData:UpdateAsync(id, {bgName, val})
--end

--game.ReplicatedStorage.DevOnlyUpdateShopOutsideServer.OnServerEvent:Connect(function(player, targetPlayer, bgName, Value)
----	updatePlayerDataFromServer(targetPlayer, bgName, Value)
--end)

local function SaveData(player)
	if player.userId < 0 then return end
	player:WaitForChild("Currency")
	wait()
	local Currency = {}
	for i, stat in pairs(player.Currency:GetChildren()) do
		table.insert(Currency, {stat.Name, stat.Value})
	end
	leaderboardData:SetAsync(player.userId, Currency)
end

local function LoadData(player)
	if player.userId < 0 then return end
	player:WaitForChild("Currency")
	wait()
	local leaderboardStats = leaderboardData:GetAsync(player.userId)
	for i, stat in pairs(leaderboardStats) do
		local currentStat = player.Currency:FindFirstChild(stat[1])
		if not currentStat then return end
		currentStat.Value = stat[2]
	end
end

players.PlayerAdded:connect(LoadData)
players.PlayerRemoving:connect(SaveData)

if SAFE_SAVE then
	game.OnClose = function()
		for i, player in pairs(players:GetChildren()) do
			SaveData(player)
		end
		wait(1)
	end
end

while AUTO_SAVE do
	wait(TIME_BETWEEN_SAVES)
	for i, player in pairs(players:GetChildren()) do
		SaveData(player)
	end
end

I’ve used datastores before and you should only save data when they leave and when they join you should fetch that data. I have no idea what this code is actually doing but, if your using ds2 it shouldn’t be that complicated, a simple game.Players.PlayerAdded should work since ds2 automatically saves data when a player leaves

1 Like

The code is iterating through an array to add data to a datastore and it saves the data as a table

1 Like

Why do it like this though? Can’t you just utilize ds2 and use it that way? unless you are making your own version of a datastore. It saves a lot of time and it does the exact same thing.

Is there a way to not use DataStore2? Because I fear that when I switch DataStores, the data would get reset