Hi! I need help with my datastore. None of the data is saving. I want both leaderstats and hiddenstats to save.
It works but it just doesn’t save…
local PlayerData = DataStoreService:GetDataStore("LumberSimulatorTestData")
local LoadData = true
local CreateData = true
local function OnPlayerJoin(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local hiddenstats = Instance.new("Folder")
hiddenstats.Name = "hiddenstats"
hiddenstats.Parent = player
local Lumber = Instance.new("IntValue")
Lumber.Name = "🪓 Lumber"
Lumber.Parent = leaderstats
local Leaves = Instance.new("IntValue")
Leaves.Name = "🍃 Leaves"
Leaves.Parent = leaderstats
local Chops = Instance.new("IntValue")
Chops.Name = "Chops"
Chops.Parent = hiddenstats
local PlayerUserId = "Player_"..player.UserId
local data = PlayerData:GetAsync(PlayerUserId)
if data and LoadData == true then
warn("Loading "..PlayerUserId.." Data!")
Lumber.Value = data["Speed"]
print("Loaded "..PlayerUserId.." Data! 👍")
elseif CreateData == true then
warn("Creating "..PlayerUserId.." Data!")
Lumber.Value = 0
Leaves.Value = 0
Chops.Value = 0
print("Created "..PlayerUserId.." Data! 👍")
else
warn("Check StoreData and CreateData, they may be off! ⚠️")
end
end
local function create_table(player)
local player_stats = {}
for _, stat in pairs(player.leaderstats:GetChildren()) do
player_stats[stat.Name] = stat.Value
end
return player_stats
end
local function OnPlayerExit(player)
local player_stats = create_table(player)
local success, err = pcall(function()
local PlayerUserId = "Player_"..player.UserId
PlayerData:SetAsync(PlayerUserId, player_stats)
end)
if not success then
local PlayerUserId = "Player_"..player.UserId
PlayerData:SetAsync(PlayerUserId, player_stats)
end
end
game.Players.PlayerAdded:Connect(OnPlayerJoin)
game.Players.PlayerRemoving:Connect(OnPlayerExit)
try and edit this to your preferences, this should work fine.
local datastores = game:GetService("DataStoreService")
local datastore = datastores:GetDataStore("DataStore")
local players = game:GetService("Players")
local function deserializeData(player, data)
local leaderstats = player.leaderstats
for statName, statValue in next, data do
local stat = leaderstats:FindFirstChild(statName)
if statName then
stat.Value = statValue
end
end
end
local function serializeData(player)
local data = {}
local leaderstats = player.leaderstats
for _, stat in ipairs(leaderstats:GetChildren()) do
data[stat.Name] = stat.Value
end
return data
end
local function onPlayerAdded(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local knockouts = Instance.new("IntValue")
knockouts.Name = "Knockouts"
knockouts.Parent = leaderstats
local wipeouts = Instance.new("IntValue")
wipeouts.Name = "Wipeouts"
wipeouts.Parent = leaderstats
local success, result = pcall(function()
return datastore:GetAsync("Stats_"..player.UserId)
end)
if success then
if result then
deserializeData(player, result)
end
else
warn(result)
end
end
local function onPlayerRemoving(player)
local data = serializeData(player)
local success, result = pcall(function()
return datastore:SetAsync("Stats_"..player.UserId, data)
end)
if success then
if result then
print(result)
end
else
warn(result)
end
end
local function onServerShutdown()
for _, player in ipairs(players:GetPlayers()) do
local data = serializeData(player)
local success, result = pcall(function()
return datastore:SetAsync("Stats_"..player.UserId, data)
end)
if success then
if result then
print(result)
end
else
warn(result)
end
end
end
players.PlayerAdded:Connect(onPlayerAdded)
players.PlayerRemoving:Connect(onPlayerRemoving)
game:BindToClose(onServerShutdown)
By hitting the stop button in studio the PlayerRemoving event won’t fire. You have to get the BindToClose() event if you want to make it save when the game apruptly closes. here is an example:
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
OnPlayerExit(player)
end
end)