My DataStore isn't working

You can write your topic however you want, but you need to answer these questions:

  1. I would like to fix my data store.

  2. Whenever I stay in the server waiting for the game to auto-save, I leave then and rejoin, but it won’t save.

  3. 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

Why are you doing this ? I don’t really see the utility :confused:

Could you show us the errors/warnings the script has? We can’t help you unless you provide us with some screenshots of the errors/warnings.

It doesn’t have errors/warnings, I would like to see what I should add.

The script would be much easier to read if you used the preformatted text feature. Just highlight your text and click on the button below.
image

sign of a code smell, it’s only about 0.03 seconds; remove it entirely or use a larger number.

why to use ipairs here

you can try creating a cache of the data and saving that after intervals or when the server shuts down , using game:BindToClose().

Edit : @BenzGamingz mind sharing the solution with relevant information to the Forum?

Never mind, I fixed it myself.

Mind sharing how you did so so that others with the same problem can reference what exactly you did to fix the problem? Stating what specifically the problem was too would help out since the thread only says “it doesn’t work”, which isn’t very informative about what the problem was.

I noticed that I had some errors in my script and I misplaced it the script in ReplicatedStorage until ServerScriptStorage.