You can write your topic however you want, but you need to answer these questions:
-
I would like to fix my data store.
-
Whenever I stay in the server waiting for the game to auto-save, I leave then and rejoin, but it won’t save.
-
I’ve tried looking in the DevForum, but no luck.
local AUTO_SAVE = true
local TIME_BETWEEN_SAVES = 60
local PRINT_OUTPUT = false
local SAFE_SAVE = true
local players = game:GetService(“Players”)
local dataStoreService = game:GetService(“DataStoreService”)
local leaderboardData = dataStoreService:GetDataStore(“leaderstats”)
local function Print(message)
if PRINT_OUTPUT then print(message) end
end
local function SaveData(player)
if player.userId < 0 then return end
player:WaitForChild(“leaderstats”)
wait()
local leaderstats = {}
for i, stat in pairs(player.leaderstats:GetChildren()) do
table.insert(leaderstats, {stat.Name, stat.Value})
end
leaderboardData:SetAsync(player.userId, leaderstats)
Print(“Saved “…player.Name…”'s data”)
end
local function LoadData(player)
if player.userId < 0 then return end
player:WaitForChild(“leaderstats”)
wait()
local leaderboardStats = leaderboardData:GetAsync(player.userId)
for i, stat in pairs(leaderboardStats) do
local currentStat = player.leaderstats:FindFirstChild(stat[1])
if not currentStat then return end
currentStat.Value = stat[2]
end
Print(“Loaded “…player.Name…”'s data”)
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