Hello. So I recently made a similar post saying that I have complaints that people sometimes just randomly lose their points and themes. This is very frustrating as I cannot seem to find what’s causing this. At first I thought it was because the ‘game:BindToClose’ needed a 10 second wait period before shutting down the server to save data, but this still did not fix the issue. Any help would be greatly appreciated
local dataStoreService = game:GetService("DataStoreService")
local bunkerThemesDataStore = dataStoreService:GetDataStore("BunkerThemes")
local lobbyThemeDataStore = dataStoreService:GetDataStore("LobbyTheme")
local currencyDataStore = dataStoreService:GetDataStore("Currency")
local function loadThemes(player)
local themesFolder = player:WaitForChild("BunkerThemes")
local themesTable
if themesFolder then
local success, err = pcall(function()
themesTable = bunkerThemesDataStore:GetAsync(player.UserId)
end)
if success and themesTable ~= nil then
for _, theme in pairs(themesTable) do
local value = Instance.new("StringValue", themesFolder)
value.Name = theme
end
end
end
end
local function saveThemes(player)
local themesFolder = player:FindFirstChild("BunkerThemes")
local themesTable = {}
local lobbyOwnerValue
local lobbyTheme
--Saving themes
if themesFolder then
local success, err = pcall(function()
--Inserting each value into the table to save
for _, theme in pairs(themesFolder:GetChildren()) do
if theme.Name ~= "Classic" then
table.insert(themesTable, theme.Name)
end
end
bunkerThemesDataStore:SetAsync(player.UserId, themesTable)
end)
end
--Saving lobby theme for the game
local success, err = pcall(function()
lobbyThemeDataStore:SetAsync(player.UserId, player.LobbyTheme.Value)
end)
end
--Loading currency
local function loadCurrency(player)
local currency
local success, err = pcall(function()
currency = currencyDataStore:GetAsync(player.UserId)
end)
print(currency)
if success and currency ~= nil then
player.leaderstats["Sanity Points"].Value = currency
end
if err then
warn(err)
end
end
--Saving currency
local function saveCurrency(player)
local success, err = pcall(function()
print(player.leaderstats["Sanity Points"].Value)
currencyDataStore:SetAsync(player.UserId, player.leaderstats["Sanity Points"].Value)
end)
if err then
warn(err)
end
end
game.Players.PlayerAdded:Connect(function(player)
local teleportingValue = Instance.new("BoolValue",player)
teleportingValue.Name = "Teleporting"
local lobbyTheme = Instance.new("StringValue",player)
lobbyTheme.Name = "LobbyTheme"
lobbyTheme.Value = ""
local themesFolder = Instance.new("Folder", player)
themesFolder.Name = "BunkerThemes"
local classicTheme = Instance.new("StringValue", themesFolder)
classicTheme.Name = "Classic"
local currencyValue = Instance.new("NumberValue", player:WaitForChild("leaderstats"))
currencyValue.Name = "Sanity Points"
local productBoughtCheck = Instance.new("BoolValue", player)
productBoughtCheck.Name = "ProductBoughtCheck"
game.ReplicatedStorage.Events:FireClient(player)
task.spawn(loadThemes, player)
task.spawn(loadCurrency, player)
game.ReplicatedStorage.NewPlayer:FireClient(player)
end)
game.Players.PlayerRemoving:Connect(function(player)
task.spawn(saveCurrency, player)
task.spawn(saveThemes, player)
end)
game:BindToClose(function()
for _, plr in pairs(game.Players:GetChildren()) do
task.spawn(saveCurrency, plr)
task.spawn(saveThemes, plr)
end
wait(5)
end)