Hello there. I currently have a very simple data store set up that saves and loads a players currency. Now I should mention that this is my first game using data stores so keep judging down to a minimum if the error is basic. So the problem is that sometimes players will just lose their stats and the stats will go back to the previous stats. For example I tested with 500 currency, I put it to 1000. I left and it said I had 1000 (in the saving pcall), but once I joined the game it said I had 500. I set up printing the data store errors in the output for saving and loading but nothing showed up… Any help would be great, thank you.
--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
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)
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(saveThemes, player)
task.spawn(saveCurrency, player)
end)
game:BindToClose(function()
for _, plr in pairs(game.Players:GetChildren()) do
task.spawn(saveThemes, plr)
task.spawn(saveCurrency, plr)
end
end)