When you use :BindToClose you want to get all the players in the game and save their stats by just doing setASync() and adding a random wait() to yield the server from stopping so it has time to store all the data.
game:BindToClose(function()
task.wait(3)
for _, plr in pairs(game:GetService("Players"):GetPlayers()) do
dataStore:SetAsync("id_"..plr.UserId, {plr.leaderstats.Coins.Value, plr.leaderstats.Gems.Value, plr.leaderstats.Wins.Value, plr.leaderstats.XP.Value})
end
end)
local dataStore = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
wait()
local plrid = "id_"..plr.UserId
local save1 = plr.leaderstats.Coins
local save2 = plr.leaderstats.Gems
local save3 = plr.leaderstats.Wins
local save4 = plr.leaderstats.XP
local GetSaved = dataStore:GetAsync(plrid)
if GetSaved then
save1.Value = GetSaved[1]
save2.Value = GetSaved[2]
save3.Value = GetSaved[3]
save4.Value = GetSaved[4]
else
local NumberForSaving = {save1.Value, save2.Value, save3.Value, save4.Value}
dataStore:GetAsync(plrid,NumberForSaving)
print("Yes")
end
end)
game:BindToClose(function()
print("no")
task.wait(3)
for _, plr in pairs(game:GetService("Players"):GetPlayers()) do
dataStore:SetAsync("id_"..plr.UserId, {plr.leaderstats.Coins.Value, plr.leaderstats.Gems.Value, plr.leaderstats.Wins.Value, plr.leaderstats.XP.Value})
end
end)
It prints the ββYesββ, but not the ββNoββ so the problem is in the last section of code
local dataStore = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
wait()
local plrid = "id_"..plr.UserId
local save1 = plr.leaderstats.Coins
local save2 = plr.leaderstats.Gems
local save3 = plr.leaderstats.Wins
local save4 = plr.leaderstats.XP
local GetSaved = dataStore:GetAsync(plrid)
if GetSaved then
save1.Value = GetSaved[1]
print("Coins")
save2.Value = GetSaved[2]
print("Gems")
save3.Value = GetSaved[3]
print("Wins")
save4.Value = GetSaved[4]
print("XP")
else
local NumberForSaving = {save1.Value, save2.Value, save3.Value, save4.Value}
dataStore:GetAsync(plrid,NumberForSaving)
end
end)
game:BindToClose(function()
task.wait(3)
for _, plr in pairs(game:GetService("Players"):GetPlayers()) do
dataStore:SetAsync("id_"..plr.UserId, {plr.leaderstats.Coins.Value, plr.leaderstats.Gems.Value, plr.leaderstats.Wins.Value, plr.leaderstats.XP.Value})
end
end)`