Hello. My datastore is not saving the data after I leave the game. Can anyone tell me what’s wrong? This is my code:
local dss = game:GetService("DataStoreService")
local ds = dss:GetOrderedDataStore("1")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
game.Players.PlayerAdded:Connect(function(player)
local playerKey = "Player_"..player.UserId
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local money = Instance.new("IntValue", leaderstats)
money.Name = "Money"
money.Value = ds:GetAsync(playerKey.."money") or 0
ds:SetAsync(playerKey.."money", money.Value)
local level = Instance.new("IntValue", leaderstats)
level.Name = "Level"
level.Value = ds:GetAsync(playerKey.."level") or 1
ds:SetAsync(playerKey.."level", level.Value)
local exp = Instance.new("IntValue", leaderstats)
exp.Name = "Exp"
exp.Value = ds:GetAsync(playerKey.."exp") or 100
ds:SetAsync(playerKey.."exp", exp.Value)
local income = Instance.new("IntValue", leaderstats)
income.Name = "Income"
income.Value = ds:GetAsync(playerKey.."income") or 0
ds:SetAsync(playerKey.."income", income.Value)
local mg1 = Instance.new("IntValue", leaderstats)
mg1.Name = "mg1"
mg1.Value = ds:GetAsync(playerKey.."mg1") or 0
ds:SetAsync(playerKey.."mg1", mg1.Value)
local mg10 = Instance.new("IntValue", leaderstats)
mg10.Name = "mg10"
mg10.Value = ds:GetAsync(playerKey.."mg10") or 0
ds:SetAsync(playerKey.."mg10", mg10.Value)
local mg100 = Instance.new("IntValue", leaderstats)
mg100.Name = "mg100"
mg100.Value = ds:GetAsync(playerKey.."mg100") or 0
ds:SetAsync(playerKey.."mg100", mg100.Value)
local mg1k = Instance.new("IntValue", leaderstats)
mg1k.Name = "mg1k"
mg1k.Value = ds:GetAsync(playerKey.."mg1k") or 0
ds:SetAsync(playerKey.."mg1k", mg1k.Value)
local mg10k = Instance.new("IntValue", leaderstats)
mg10k.Name = "mg10k"
mg10k.Value = ds:GetAsync(playerKey.."mg10k") or 0
ds:SetAsync(playerKey.."mg10k", mg10k.Value)
local mg100k = Instance.new("IntValue", leaderstats)
mg100k.Name = "mg100k"
mg100k.Value = ds:GetAsync(playerKey.."mg100k") or 0
ds:SetAsync(playerKey.."mg100k", mg100k.Value)
local function updateStats()
ds:SetAsync(playerKey.."money", money.Value)
ds:SetAsync(playerKey.."level", level.Value)
ds:SetAsync(playerKey.."exp", exp.Value)
ds:SetAsync(playerKey.."income", income.Value)
ds:SetAsync(playerKey.."mg1", mg1.Value)
ds:SetAsync(playerKey.."mg10", mg10.Value)
ds:SetAsync(playerKey.."mg100", mg100.Value)
ds:SetAsync(playerKey.."mg1k", mg1k.Value)
ds:SetAsync(playerKey.."mg10k", mg10k.Value)
ds:SetAsync(playerKey.."mg100k", mg100k.Value)
print("Updated")
end
while true do
wait(1)
money.Value = money.Value + income.Value
end
money.Changed:Connect(function()
if money.Value > exp.Value then
level.Value = level.Value + 1
exp.Value = exp.Value*1.2
print("money is greater than exp")
end
end)
game:BindToClose(function()
-- if the current session is studio, do nothing
if RunService:IsStudio() then
return
end
print("saving player data")
-- go through all players, saving their data
local players = Players:GetPlayers()
for _, player in pairs(players) do
local userId = player.UserId
if 1==1 then
-- wrap in pcall to handle any errors
local success, result = pcall(function()
-- SetAsync yields so will stall shutdown
ds:SetAsync(playerKey.."money", money.Value)
ds:SetAsync(playerKey.."level", level.Value)
ds:SetAsync(playerKey.."exp", exp.Value)
ds:SetAsync(playerKey.."income", income.Value)
ds:SetAsync(playerKey.."mg1", mg1.Value)
ds:SetAsync(playerKey.."mg10", mg10.Value)
ds:SetAsync(playerKey.."mg100", mg100.Value)
ds:SetAsync(playerKey.."mg1k", mg1k.Value)
ds:SetAsync(playerKey.."mg10k", mg10k.Value)
ds:SetAsync(playerKey.."mg100k", mg100k.Value)
print("Updated")
end)
if not success then
warn(result)
end
end
end
print("completed saving player data")
end)
game.Players.PlayerRemoving:Connect(updateStats)
end)