So I have a script in ServerSriptStorage where I save all my tools when the backpack changes and when a Humanoid dies. However, some tools are lost when I die or leave. Please help.
local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("InventorySaveSystem")
local RS = game:GetService("ReplicatedStorage")
local function getTools(plr)
print(plr.Backpack)
print("getting tools")
local savedTools
local success, errormsg = pcall(function()
savedTools = DS:GetAsync("plr_".. plr.UserId)
end)
if success then
print("Player data received save data")
else
print("Failed at loading data")
warn(errormsg)
while not success do
repeat
wait(3)
success, errormsg = pcall(function()
savedTools = DS:GetAsync("plr_".. plr.UserId)
end)
until success == true
end
end
if savedTools ~= nil then
for i = 1,#savedTools do
local items = RS.Items
if items:FindFirstChild(savedTools[i]) then
local clone = items[savedTools[i]]:Clone()
clone.Parent = plr.Backpack
elseif items:WaitForChild("Swords"):FindFirstChild(savedTools[i]) then
local clone = items.Swords:WaitForChild(savedTools[i]):Clone()
clone.Parent = plr.Backpack
end
end
end
end
local function saveTool(plr)
print("saving tools")
local saveTools = {}
local tools = plr.Backpack:GetChildren()
print(plr.Backpack)
for i = 1, #tools do
table.insert(saveTools,tools[i].Name)
end
if plr.Character then
if plr.Character:FindFirstChildOfClass("Tool") then
local chartool = plr.Character:FindFirstChildOfClass("Tool")
table.insert(saveTools, chartool.Name)
end
end
local success, errormsg = pcall(function()
DS:SetAsync("plr_".. plr.UserId,saveTools)
end)
if success then
print("Player data got saved")
else
print("Failed at saving data")
warn(errormsg)
if not success then
repeat
wait(3)
success, errormsg = pcall(function()
DS:SetAsync("plr_".. plr.UserId,saveTools)
end)
until success == true
end
end
end
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
wait()
if DS:GetAsync("plr_".. plr.UserId) ~= nil then
print("Getting Tools")
getTools(plr)
else
local axeClone = RS.Items.Swords["Wooden Axe"]:Clone()
axeClone.Parent = plr.Backpack
local branchClone = RS.Items.Sword["Branch"]:Clone()
branchClone.Parent = plr.Backpack
local pickaxeClone = RS.Items.Sword["Wooden Pickaxe"]:Clone()
pickaxeClone = plr.Backpack
saveTool(plr)
end
end)
local characterTool
local char = plr.Character or plr.CharacterAdded:Wait()
char.ChildAdded:Connect(function(tool)
if tool:IsA("Tool") then
characterTool = tool
saveTool(plr)
end
end)
char.ChildRemoved:Connect(function(tool)
if tool:IsA("Tool") then
local tools = plr.Backpack:GetChildren()
local found
for i = 1,#tools do
if tools[i] == tool then
found = true
end
end
if not found then
saveTool(plr)
end
end
end)
plr.Backpack.ChildAdded:Connect(function(tool)
if tool:IsA("Tool") then
if tool ~= characterTool then
saveTool(plr)
else
print("Character Tool")
end
end
end)
local db = true
char.Humanoid.Died:Connect(function()
saveTool(plr)
end)
plr.Backpack.ChildRemoved:Connect(function(tool)
if db == true then
db = false
wait(0.3)
if plr.Character and tool.Parent ~= plr.Character then
saveTool(plr)
end
db = true
end
end)
end)
