function LoadMisc(plr)
local key = plr.UserId
local pets = plr:WaitForChild("Pets")
local Data
local succ, err = pcall(function()
data = MiscStorage:GetAsync(key)
end)
if succ then
for i,v in pairs(pets:GetChildren()) do
v.Value = data[v.Name]
end
else
warn(err)
end
end
function SaveMisc(plr)
local pets = plr:FindFirstChild("Pets")
local key = plr.UserId
local Data = {}
for i,v in pairs(pets:GetChildren()) do
Data[v.Name] = v.Value
end
local succ, err = pcall(function()
MiscStorage:SetAsync(key, Data)
end)
if succ then
print("succesfully saved misc items for " .. key)
else
warn(err)
end
end
Players.PlayerAdded:Connect(LoadMisc)
Players.PlayerRemoving:Connect(SaveMisc)
This code works perfectly fine but I kind of want to start using UpdateAsync. How would I do so?
function SaveMisc(plr)
local pets = plr:FindFirstChild("Pets")
local key = plr.UserId
local Data = {}
for i,v in pairs(pets:GetChildren()) do
Data[v.Name] = v.Value
end
local succ, err = pcall(function()
if Data then
MiscStorage:UpdateAsync(key, function(oldValue)
local previousdata = oldValue or {DataId = 0}
if Data.DataId == previousdata.DataId then
Data.DataId = Data.DataId + 1
return Data
else
return nil
end
end
end)
if succ then
print("succesfully saved misc items for " .. key)
else
warn(err)
end
end
I Recommend Using a Bindable Event if a Server Unexpectedly Shutsdown or the player crashes.
Also Add local playersleft = 0
Then Add +1 to the 0 (if you have PlayerAdded Event)
NOTE: this will work if you have a PlayersRemoving Section.
local bindableEvent = Instance.new('BindableEvent')
--Players Removing Section--
game:BindToClose(function()
while players left >0 do
bindableEvent.Event:Wait()
end
end)