Keep Inventory Script (HELP)

I have tested this script and it works, also you could use StarterGear instead but it’s up to you and don’t use DataStore for this, use it only when you have to save something when the player leaves the game, not when they die.

local PlayersTools = {}

function OnCharacterAdded(Character)
	task.wait(1)
	local Humanoid = Character:WaitForChild("Humanoid")
	local Player = game.Players:GetPlayerFromCharacter(Character)
	Humanoid.Died:Connect(function()
		local NewFolder = Instance.new("Folder",game.ServerStorage)
		NewFolder.Name = Player.Name.."'s Tools"
		for _, Tool in pairs(Character:GetChildren()) do
			if Tool:IsA("Tool") then
				Tool.Parent = NewFolder
			end
		end
		for _, Tool in pairs(Player.Backpack:GetChildren()) do
			if Tool:IsA("Tool") then
				Tool.Parent = NewFolder
			end
		end
		repeat wait() until Player.Character ~= Character
		for _, Tool in pairs(NewFolder:GetChildren()) do
			Tool.Parent = Player.Backpack
		end
		NewFolder:Destroy()
	end)
end

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(OnCharacterAdded)
	if Player.Character then OnCharacterAdded(Player.Character) end
end)

local plrs = game:GetService("Players")

plrs.PlayerAdded:Connect(function(plr)
	local inventory = Instance.new("Folder", plr)
	inventory.Name = "Inventory"
	
	local tools = {}
	local function toolAdded(tool: Tool)
		if tool:IsA("Tool") then
			if table.find(tools, tool) then return end
			tools[#tools+1] = tool
			
			tool:Clone().Parent = inventory
		end
	end
	
	local childAdded = {}
	
	plr.CharacterAdded:Connect(function(char)
		for _, conn in childAdded do
			if typeof(conn) == "RBXScriptConnection" then
				conn:Disconnect()
			end
		end
		
		local backpack = plr:WaitForChild("Backpack")
		childAdded[#childAdded+1] = backpack.ChildAdded:Connect(toolAdded)
		childAdded[#childAdded+1] =  char.ChildAdded:Connect(toolAdded)
		
		for _, tool in inventory:GetChildren() do
			tool.Parent = backpack
		end
		tools = {}
	end)
end)