I recently though of creating an auto save feature which will save player's data after every 2 minutes. I did it like this inside the player added event:
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Checkpoint = Instance.new("IntValue", leaderstats)
Checkpoint.Name = "Stage"
Checkpoint.Value = 0
local coins = Instance.new("IntValue", leaderstats)
coins.Name = "Coins"
coins.Value = 0
local level = Instance.new("IntValue", leaderstats)
level.Name = "Level"
level.Value = 1
local rebirth = Instance.new("IntValue", leaderstats)
rebirth.Name = "Rebirth"
rebirth.Value = 0
--Data Saving
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId)
end)
if success and data then
coins.Value = data.Coins
Checkpoint.Value = data.Checkpoint
level.Value = data.Level
rebirth.Value = data.Rebirth
end
-- This is the part where I implement the auto save feature.
while true do
wait(AUTO_SAVE_TIME) -- I have already set the 'AUTO_SAVE_TIME' to 120 seconds above. Just didn't show it.
for i, v in pairs(game.Players:GetChildren()) do
save(player) -- I also have a save function which just saves the player's data. That function is working properly. I am just passing the player argument because the function requires that.
end
end
So is this the right way to do it? Although I am getting DataStore Request was added to queue after doing like this. Isn't 2 minutes (120 seconds) enough for SetAsync()?
Thanks for any help
