I'm trying to get my script to always save my data, but it's inconsistent in doing so. I've tried pcalls, while loops and repeats, none of which have worked. Is there something I'm doing wrong? I can't figure it out, I've looked into a lot of articles about datastore and I've asked multiple people about this. Here's the entire datastore script.
geldSave = game:GetService('DataStoreService'):GetDataStore('geldSave')
chibiSave = game:GetService('DataStoreService'):GetDataStore('chibiSave')
game.Players.PlayerAdded:Connect(function(plr)
wait(1)
local geld = Instance.new('IntValue')
geld.Name = 'ChibiCoins'
geld.Value = geldSave:GetAsync(plr.UserId) or 100
geld.Parent = plr
local ownedChibi = Instance.new('Folder')
ownedChibi.Name = 'OwnedChibi'
ownedChibi.Parent = plr
chibiSave:GetAsync(plr.UserId)
local chibi = chibiSave:GetAsync(plr.UserId)
if chibi then
local chibiclone = chibi:GetChildren():Clone()
chibiclone.Parent = plr.OwnedChibi
else
return nil
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local chibis = plr.OwnedChibi:GetChildren()
local saved = false
while saved == false do
chibiSave:SetAsync(plr.UserId, game:GetService('HttpService'):JSONEncode(chibis))
geldSave:SetAsync(plr.UserId, plr.ChibiCoins.Value)
wait()
saved = true
end
print('saved')
end)
