Every few times a player leaves, it wont either save, or retrieve the backpack data, and I’m looking for ways to prevent this, here’s the script, if you have any thoughts do tell:
local DataStore = game:GetService("DataStoreService")
local CurrentData = DataStore:GetDataStore("DataNameHere")
local lastItem
game.Players.PlayerAdded:Connect(function(Player)
local Data
local success, whoops = pcall(function()
Data = CurrentData:GetAsync(Player.UserId)
end)
if success and Data then
for _, Tool in pairs(Data) do
local ToolCheck = game.ReplicatedStorage.Items:FindFirstChild(Tool)
if ToolCheck then
local ToolClone = ToolCheck:Clone()
ToolClone.Parent = Player.Backpack
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local lastItemHeld = Player:WaitForChild("LastItemHeld")
lastItem = lastItemHeld.Value
local ToolsToSave = {}
for _, Tool in pairs(Player.Backpack:GetChildren()) do
table.insert(ToolsToSave, Tool.Name)
end
table.insert(ToolsToSave, lastItem)
local success, whoops = pcall(function()
CurrentData:SetAsync(Player.UserId, ToolsToSave)
end)
if success then
print("Data saved")
else
warn(whoops)
end
end)