So my datastore doesn't save all the variables when the last player leaves so when the player would buy ingame purchases using R$ it won't always save
-- Player Profile creator
local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerStats")
game.Players.PlayerAdded:connect(function(plr)
local profile = Instance.new("Folder")
profile.Parent = game.Workspace.PlayerProfiles
profile.Name = plr.Name
local profile2 = Instance.new("NumberValue")
profile2.Name = "BuyableCurrency"
profile2.Parent = profile
--loading stats
--plr:WaitForDataReady()
wait(2)
local stats2 = game.Workspace.PlayerProfiles:FindFirstChild(plr.Name):GetChildren()
for i = 1, #stats2 do
stats2[i].Value = DataStore:GetAsync(stats2[i].Name)
print(stats2[i].Value)
if p12.Value == 0 then
p12.Value = 15
end
end
end)
game.Players.PlayerRemoving:connect(function(plr2)
local stats = game.Workspace.PlayerProfiles:FindFirstChild(plr2.Name):GetChildren()
for i=1, #stats do
print(stats[i].Name)
DataStore:SetAsync(stats[i].Name, stats[i].Value)
end
end)
what I'm trying to do is when the game is closing to have a different way of saving the last players values without an issue occuring
how would I use
Game.OnClose = function()
in this case to save player variables in a datastore?

1 comment
game.OnClose = function() wait(15) end