Hi, I have a script in the ServerScriptService that saves the users inventory on death, but the function for Player.BackpackChildAdded is not firing (printing out anything) and there are no errors. Please tell me on where I have gone wrong. Thank you.
Part of the script that’s important:
local characterTool = nil
local backpack = plr.Backpack
backpack.ChildAdded:Connect(function(tool)
if tool:IsA("Tool") then
if tool ~= characterTool then
print("Inserting ".. tool.Name .." into table")
table.insert(toolsTable,tool.Name)
end
end
end)
backpack.ChildRemoved:Connect(function(tool)
if tool:IsA("Tool") then
if characterTool and tool ~= characterTool then
if plr.Character and plr.Character.Humanoid.Health > 0 then
for i = 1,#toolsTable do
if toolsTable[i] == tool.Name then
print("Removing ".. tool.Name .." from table")
table.remove(toolsTable,i)
break
end
end
end
end
end
end)
local char = plr.Character or plr.CharacterAdded:Wait()
char.ChildAdded:Connect(function(tool)
if tool:IsA("Tool") then
characterTool = tool
end
end)
char.ChildRemoved:Connect(function(tool)
wait(0.01)
if tool.Parent ~= backpack then
if plr.Character and plr.Character.Humanoid.Health > 0 then
for i = 1,#toolsTable do
if toolsTable[i] == tool.Name then
print("Removing ".. tool.Name .." from table")
table.remove(toolsTable,i)
break
end
end
end
end
end)
char:WaitForChild("Humanoid").Died:Connect(function()
DS:SetAsync("plr_".. plr.UserId,toolsTable)
print("Inventory Saving")
end)