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?
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)
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).
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.
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.
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.
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)