Hi, thank you for reading. So, I'm making a game and I want to be able to save stats. I tried reusing my old save scripts, but they don't work anymore, so, I found an autosave script from the toolbox and thought, "Why not?" I put it in serverscriptservice, turned on HTTP and Enabled Studio Access, but of course, nothing works. The game is a place inside of a game (If you know what I mean), and it's a normal script. I have a leaderstats script in Workspace and the autosave script in the serverscriptservice. Before you say, "Oh, she has to put the leaderstats in serverscript service," it's not that script that doesn't work, it's the autosave. I also did put the leaderstats in the serverscriptservice, but it still didn't work. Autosave (I didn't make):
local AUTO_SAVE = true -- Make true to enable auto saving
local TIME_BETWEEN_SAVES = 60 -- In seconds (WARNING): Do not put this lower than 60 seconds
local PRINT_OUTPUT = false -- Will print saves and loads in the output
local SAFE_SAVE = true -- Upon server shutdown, holds server open to save all data
---------------------------------
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
Leaderstats script (It works, it shows the leaderstats, but if you want to see it, here):
function onPlayerEntered(newPlayer)
wait(.5)
local stats = Instance.new("IntValue")
stats.Name = "leaderstats"
local Girl = Instance.new("IntValue")
Girl.Name = "Girl"
Girl.Value = 0
local Boy = Instance.new("IntValue")
Boy.Name = "Boy"
Boy.Value = 0
local Hair1 = Instance.new("IntValue")
Hair1.Name = "Hair1"
Hair1.Value = 0
Hair1.Parent = stats
Boy.Parent = stats
Girl.Parent = stats
stats.Parent = newPlayer
end
game.Players.ChildAdded:connect(onPlayerEntered)
Thank you!
