How to prevent data loss if I want to save every time coins save

So, I want to save my coins every time it changes because I don’t like auto saving, because you have to wait like 2 minutes to save data, y’know waiting is booring. so I wanna save data every time the coins changing without data loss, how would I do THAT?

Don’t tell me DataStore2, I don’t like their backing up method. So what’s the best method to ensure no data loss?

1 Like

Here’s a simple method:

local function saveData(player, data)
	-- INS Code Here
end

game:GetService('Players').PlayerRemoving:Connect(function(player)
	saveData(player, ...)
end)

game:BindToClose(function()
	for index, player in pairs(game:GetService('Players'):GetPlayers()) do
		task.spawn(function()
			saveData(player, ...)
		end)
	end
end)

Also, what’s wrong with their backing?

2 Likes

AFAIK, Datastore2 and it’s saving method is probably the best you’re going to get, if you’re really that paranoid about the generally rare event of data loss, and don’t want to spend any money on external servers. I mean, Roblox itself does backups (unfortunately I can’t find the post I got that info from).

1 Like

I don’t like how they create a new datastore for 1 user just to save every time the data is saved, it’s like abusing a newborn datastore

And that is why I made this post to see if there’s something EVEN better

Most likely not, it’s a game of trade-offs. You’re just going to have to deal with one or the other.

It would be next to impossible to lose data with Datastore2, because the event of multiple datastores getting corrupted is probably never going to happen. Of course, it is still kind of overkill. I’ve personally never run into any sort of data loss issues, so I don’t really see the point.

what if I do the saving method, but in one datastore? I’m pretty sure it wouldnt be that much of an overkill

Or should I just use listversionsasync to retrieve older versions

Also, that’s just the backing up, I want to know how to save it too

Yeah, you could do that, but it probably wouldn’t be better than Datastore2, because it’s just the exact same method with just a few changes to make it use 1 datastore.

Just use ProfileService for this goal?

i think that only serves session locking which i dont really need as i dont have items, just coins

It’s efficient and effective data saving that just so happens to have session-locking built into it. It basically gives you a table called a profile and anything you put in the table automatically saves, I’ve never had any reports of data loss with hundreds of thousands of visits and neither has loleris.

2 Likes

i guess ill try that, if it works ill mark this as solution

You could just save the data whenever the “PlayerRemoving” event is fired for a leaving player, this circumvents the need for auto-updating.

local Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("DataStore")

local PlayersDoNotSave ={}

local function loadData(Player)
	local succ, res = pcall(function()
		return DS:GetAsync("Coins_"..Player.UserId)
	end)

	if succ then
		if res then
			return res
		else
			return 0
		end
	else
		table.insert(PlayersDoNotSave, Player)
		warn(res)
	end
end

local function saveData(Player)
	if table.find(PlayersDoNotSave, Player) then
		return
	end
	
	local succ, res = pcall(function()
		return DS:SetAsync("Coins_"..Player.UserId)
	end)
	
	if succ then
		return
	else
		warn(res)
	end
end

local function onPlayerJoined(Player)
	local ls = Instance.new("Folder")
	ls.Name = "leaderstats"
	ls.Parent = Player

	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = ls
	coins.Value = loadData(Player)
end

local function onServerShutdown()
	for _, Player in ipairs(Players:GetPlayers()) do
		if table.find(PlayersDoNotSave, Player) then
			continue
		end
		saveData(Player)
	end
end

Players.PlayerAdded:Connect(onPlayerJoined)
Players.PlayerRemoving:Connect(saveData)
game:BindToClose(onServerShutdown)
1 Like

What if outages, or datastore broke? Also this was already solved

The script I provided avoids the loss of data.